Show selected value when editing record

Viewed 116

When editing a record, I want the dropdown to show the previously selected value.

Code is 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);
)};
)};

To clarify, I want the previously selected value, that which is present in the record, to be selected in dropdown when a user edits the record. Any help is appreciated.

2 Answers

You need to specify the selected tag, otherwise it will just populate the dropdown.

So, while looping the value, if you need to check which value has to be selected (if it is user specific).

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

You need to check for the previously selected value. Please see the code below:

<?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>'
    }   
}    
?>

Replace previously selected with the relevant check to see if the value was the previously selected value. If you require assistance with this, I will need to see the record side of things, if so, let me know.

Related