javascript - on dropdown Change event load data from database and fill text box

623

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

796

Answer

Solution:

You need to assign an event listener for your select:

<select name='selectLoco' id='selectLoco' onchange="$('#textboxid').val(this.value);">

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:

<select name='selectLoco' id='selectLoco' onchange="updateValue();">
    <option value="123">aaa</option>
    <option value="456">bbb</option>
</select>
<input type="text" id="textboxid" />

<script type="text/javascript">
//the function that takes care of the updating
function updateValue()
{
    $('#textboxid').val($('#selectLoco').val());
}

//update once the page has loaded too
$(document).ready(function () {
    updateValue();
});
</script>
725

Answer

Solution:

I got a work around , based on the answers using ajax + json here its how i did it

The jquery

$('#selectLoco').on('change', function(){
    selectLoco = $('#selectLoco option:selected').val(); // the dropdown item selected value
    $.ajax({
        type :'POST',
        dataType:'json',
        data : { selectLoco : selectLoco },
        url : 'getresult.php',
        success : function(result){
         $('#textBoxIwant).val(result['WeekDaySm']);// json result      
        }
    });

});

The PHP

<?php 
require_once ('../config/db.config.php');

$selectLoco = $_POST['selectLoco'];
$query = "SELECT * FROM misc_location WHERE id = '$selectLoco' ";

$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo json_encode($row);
 ?>
311

Answer

Solution:

Can you try this, usingonchange firing event you can able to call javascript function and you populate the value.myvalue you can replace your textbox id instead.

  $selectLoco = "<select name='selectLoco' id='selectLoco'  onchange='UpdateTextbox(this)'>";
     while ($row = mysql_fetch_row($result) ) {
        $selectLoco .= "<option value='$row[0]'> $row[1] </option>";
     }
     $selectLoco .= "</select>";

  echo '<input id="myvalue" type="text">';

javscript:

 function UpdateTextbox(d){  
          document.getElementById('myvalue').value = d.value;
 }

if using Jquery to implement:

   $(function(){
    $('#selectLoco').change(function(){
       $("#myvalue").val($(this).val());
     });
   });
584

Answer

Solution:

Add a custom data attribute to the select to representing time and then access it with jQuery.

$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]' data-time='$row[2]'> $row[1] </option>";
     }
     $selectLoco .= "</select>";
} 

Jquery below:

$('#selectLoco').on('change',function(e){
    $('#textboxid').val($(this).children(':selected').attr('data-time'))
});

JSfiddle here

People are also looking for solutions to the problem: php - how to get avatar on front end wordpress

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.