javascript - on dropdown Change event load data from database and fill text box
I have a simple dropdown list, whose values are from the DB.I am doing it as
$query = "SELECT id,location_name FROM misc_location";
if ( $result = mysql_query($query) ) {
$selectLoco = "<select name='selectLoco' id='selectLoco'>";
while ($row = mysql_fetch_row($result) ) {
$selectLoco .= "<option value='$row[0]''> $row[1] </option>";
}
$selectLoco .= "</select>";
}
But now i have a text box below the dropdown. So when user selects a dropdown item ,I need to pull a value corresponding the item selected and display in the text box.
Table : misc_location
id location_name time
1 Ben 7.00 am
2 Cat 8.00
Location : Drop-down user selects Ben , Text box below should display 7.00
Answer
Solution:
You need to assign an event listener for your select:
And correct your php. You have the quotes twice where you output the option value. You might also want to update the value on load.
Here's a jsfiddle link
And a rather better example:
Answer
Solution:
I got a work around , based on the answers using ajax + json here its how i did it
The jquery
The PHP
Answer
Solution:
Can you try this, using
onchange
firing event you can able to call javascript function and you populate the value.myvalue
you can replace your textbox id instead.javscript:
if using Jquery to implement:
Answer
Solution:
Add a custom data attribute to the select to representing time and then access it with jQuery.
Jquery below:
JSfiddle here