编辑记录时显示选定值

9avjhtql  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(342)

编辑记录时,我希望下拉列表显示以前选择的值。
代码是below:-

<div class="modal fade" id="edit_model" tabindex="-1" role="dialog"  aria-hidden="true" style="display: none;">
  <div class="modal-dialog modal-lg">
     <div class="modal-content">
        <div class="modal-header">
           <h6 class="modal-title" >Model Title</h6>
           <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        </div>
        <div class="modal-body">
           <form action="filename.php" method="post">
              <div class="form-group row">
                 <div class="col-md-1"></div>
                 <div class="col-md-3">
                    <input type="text" name="first_name" placeholder="First Name" id="first_name" class="form-control rs_input" required >
                 </div>
                 <div class="col-md-3">
                    <input type="text" name="last_name" placeholder="Last Name" id="last_name" class="form-control rs_input" required >
                 </div>
                 <select class="form-control" id="dropdown_id" name="dropdown_name">
                       <option >Select Value</option>

                       <?php
                          $dropdown = mysqli_query($connect, "SELECT * FROM table") or die(mysqli_error($connect));
                          while ($fetch_data = mysqli_fetch_array($country)) {
                              ?>
                       <option value="<?php echo $fetch_data['id']; ?>"><?php echo $fetch_data['name']; ?></option>
                       <?php
                          } ?>
                    </select>
              </div>
             </div>
        </div>
    </div>
</div>

jquery:-

$('.class_name').on('dblclick',function(){
var value = $(this).attr("id");

$.ajax({
  url:"ajax/filename.php",
  method:"POST",
  data:{value:value},
  dataType:"json",
  success:function(data){

    $('#first_name').val(data.first_name);
    $('#last_name').val(data.last_name);
    $('#dropdown_id').val(data.dropdown_value);
)};
)};

为了澄清,我希望在用户编辑记录时,在下拉列表中选择先前选择的值,即记录中存在的值。感谢您的帮助。

s3fp2yjn

s3fp2yjn1#

您需要指定 selected 标签,否则它将只填充下拉列表。
因此,在循环值时,如果需要检查必须选择哪个值(如果它是特定于用户的)。

if (some condition)
{
  <option selected="selected" ...
}
else
{
  <option ...
}
imzjd6km

imzjd6km2#

您需要检查以前选择的值。请参见下面的代码:

<?php
$dropdown = mysqli_query($connect, "SELECT * FROM table") or die(mysqli_error($connect));
while ($fetch_data = mysqli_fetch_array($country)) 
{
    if(*previously selected*)
    {
        echo '<option selected value="<?php echo $fetch_data['id']; ?>"><?php echo $fetch_data['name']; ?></option>'
    }
    else
    {
        echo '<option value="<?php echo $fetch_data['id']; ?>"><?php echo $fetch_data['name']; ?></option>'
    }   
}    
?>

用相关检查替换先前选择的值,以查看该值是否为先前选择的值。如果你需要这方面的帮助,我将需要看到事情的记录面,如果是这样,让我知道。

相关问题