简体   繁体   中英

PHP / AJAX - Populate 3 textboxes with dynamic data based on select list

Im trying to find a tutorial or something that will allow me to populate 3 textboxes with data from a mysql database based on whats clicked on in a select dropdown box.

For example, my select looks like the following:

<select name="imaselect">
    <option value="USA">USA</option>
    <option value="AUS">AUS</option>
    <option value="NZ">NZ</option>
</select>

Then under that i have 3 blank textboxes.

If i click USA, it will go to the database, ask for 3 values, return them and then put them in the 3 text boxes, same if i click AUS and the same if i click NZ.

Any help would be greatly appreciated, im having trouble with this.

Cheers,

You need 2 things.

  1. a php file that connects to the database and retrieve the three values for the country passed to it as a parameter.
  2. a javascript script that calls the php file with country name and puts the returned values in the textboxes.
<script type="text/javascript">
    jQuery(function( $ ){    
        $("select").change(function(e) {    
            $(".temp").load(\'index.php?parameter1=\'+ $(this).val(), , function() {
                $(".textbox1").val($(".temp1").text());
            });
        });
    });
</script>

Something like this. Of course you have to set a reference to the jquery file. And then you have an index.php (or name it different) and can give it parameters however you want via the load function. The element with the class temp will get the content, because you cant use the load to get data in the textbox directly. (So you need to create that element .. hidden!) Then it gets copied into the element with the class textbox1. copy the 2 inner lines as many times as you want (just use like textbox2 instead, temp can stay the same)

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