PHP / AJAX - 动态文本框值

PHP / AJAX  - 动态文本框值

问题描述:

Im having trouble trying to figure out some code someone has suggested and was hoping someone could explain it a bit more, or suggest something easier.

I have 3 textboxes which i need to fill with dynamic data from a MySQL database, which data is shown is dependant on what the user clicks in a select dropdown box.

The code i have is the following:

<script type="text/javascript">
 jQuery(function( $ ){    
  $("select").change(function(e) {  
   $(".temp").load(\'index.php?parameter1=\'+ $(this).val(), , function() {
    $(".textbox1").val($(".temp1").text());
   });
  });
 });
</script>

Im not quite sure how to use javascript so im unsure of how this works, and also what code would go into the index.php file?

Any help would be greatly appreciated.

Cheers

Yea this is from me and it is quite simple.

First 2 lines are just initialisation, no need to change ever.

3rd is a change trigger on "select", so all code inside this function will be executed when a selectbox changes its value. Using a class or id (.x, #x) would be better but in you example was none given.

4th Line loads the content of the given url in that case eg: "index.php?parameter1=USA" into an element (div or whatever) that has the class temp.

Finally in the 5th line this temporarly loaded content will be copyed in the textbox with the class texbox1.

For this to work you need to include the jquery file, sth like this:

<script src="jquery-latest.js" type="text/javascript"></script>

Also there has to be: the given select, a textbox with the class textbox1, and a div (hidden) with the class temp

In the index.php (or name it textboxvalues.php .. whatever) you just have to echo the values that you want to have in the textbox. You can use a switch or if for the parameters (for the differen textboxes) and get the values from a database or however you want. You can even use a different php file for each textbox. Whatever you want.

Ok, i recommed you to cleanup the code a bit:

<script type="text/javascript">
$(document).ready(function(){
    $("select").change(function(e) {
        var url = 'index.php?paremeter1=' + $(this).val();
        $(".textbox1").load(url);
    });
});
</script>

Eventually you should change $("select") to $(".select") or $("#select").