Hi,

I have a form which consists a dynamic dependent dropdown and I want to maintain the entered data into the fields after the form is saved or edited.

For an example:
<input type="text" name="serial" value="<?php echo $serial;?>">

Similary i need to get the value in my dropdown as well. Hope there is a solution for this. Below is my code.

ajax.js
$(document).ready(function() {
    $("#brand").change(function() {
    var Brand_ID = $(this).val();
    if(Brand_ID != "") {
      $.ajax({
        url:"SR.php",
        data:{b_id:Brand_ID},
        type:'POST',
        success:function(response) {
          var resp = $.trim(response);
          $("#model").html(resp);
        }
      });
    } else {
      $("#model").html("<option value=''>----Select Model----</option>");
    }
  });
});
Dropdown
    $Connection = mysqli_connect('localhost','root','','sris');

<form class="form-horizontal style-form" action="update.php" method="post">
<div class="form-group">
        <label class="col-sm-2 control-label">Brand</label>
    <div class="col-sm-4">
        <select name="Brand" id="brand" class="form-control" value="<?php echo $Brand; ?>" >
                        <option value=''>----Select Brand----</option>
                          <?php 
                                $sql = "select * from brand";
                                    $res = mysqli_query($Connection, $sql);
                                        if(mysqli_num_rows($res) > 0) {
                                    while($row = mysqli_fetch_object($res)) {
                                        echo "<option value='".$row->ID."'>".$row->Name."</option>";
                                    }
                                }
                            ?>
        </select>
    </div>

        <label class="col-sm-2 control-label">Model</label>
    <div class="col-sm-4">
        <select name="Model" id="model" class="form-control" value="<?php echo $Model; ?>" >
            <option>----Select Model----</option></select>
                <?php
                if(isset($_POST['b_id'])) {
                  $sql = "select * from model where Brand_ID =".mysqli_real_escape_string($Connection, $_POST['b_id']);
                  $res = mysqli_query($Connection, $sql);
                  if(mysqli_num_rows($res) > 0) {
                    echo "<option value=''>----Select Model----</option>";
                    while($row = mysqli_fetch_object($res)) {
                      echo "<option value='".$row->ID."'>".$row->Name."</option>";
                    }
                  }
                  exit();
                }
                ?>
    </div>
</div>
</form>

Recommended Answers

All 2 Replies

<option>----Select Model----</option>
<?php
  if(isset($_POST['b_id'])) {
    $sql = "SELECT * FROM model WHERE Brand_ID =".mysqli_real_escape_string($Connection, $_POST['b_id']);
    $res = mysqli_query($Connection, $sql);
    if(mysqli_num_rows($res) > 0) {
        echo "<option value=''>----Select Model----</option>";
        while($row = mysqli_fetch_assoc($res)) {
          echo "<option value=".$row['ID'].">".$row['name']."</option>";
          //echo "ID is: ".$row[ID]." and name is: ".$row[name];
        }
    }
  }
?>
</select>

Please Shara SR.php file for better response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.