简体   繁体   中英

how to bind data to text box according change value of select in php

i have two input field.one is select and another is input type text.

i want to show all district name from the database to select element and i get the id of district name to the text box. please help.hear is my code, i don't understand how to bind district id to text box.

<tr> <td><label>Select District</label></td>
<td>
<select>
<option>--Select--</option>
<?php
$sql = "SELECT * FROM `district`";
$query = mysqli_query($con, $sql);
while ($data = mysqli_fetch_array($query)) {
?>
<option value="<?php echo $data["districtname"]; ?>"> <?php echo $data["districtname"]; ?></option>`enter code here`
 <?php
}
 ?>
 </select>    
     </td>
          </tr>

 <tr>
 <td><label>District ID</label></td>
   <td>
        <input type="text" name="distrctID" disabled="disabled" value="<?php echo $data["DistrictID"]; ?>" />
   </td>
   </tr>

If you want the selected district id should be in the text box then this will help

your option should be like

<option value="<?php echo $data["districtID"]; ?>"><?php echo $data["districtname"]; ?></option>
    <input type="text" name="distrctId" disabled="disabled" id="districtId" />

and a javascript on change of select should be like

$('select').on('change', function (e) {
var optionSelected = $("option:selected", this);
var valueSelected = this.value;
$('#districtId').val(valueSelected);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM