php ajax database question
I am trying to understand how do I modify the example in this http://www.w3schools.com/php/p开发者_Python百科hp_ajax_database.asp so that the database is queried for multiple selections. I mean, if more than one option is selected (by adding the "multiple" keyword to the form), how do I do a database query for these multiple selections?
Without looking at that page, to send multiple values from a select, just add []
right after the name field: <select multiple="multiple" name="foo[]">
.
The request array ($_GET or $_POST in PHP) will contain a key 'foo' which maps to multiple 'selected' values. Once you have modified your page, to get a clearer idea of what is going on, just do:
print_r($_GET['foo']); // or $_POST, whichever
Then you will just need to modify the server script to store (or do whatever) with the set of received values.
The following example demonstrates how to create a string from the multiple selected values, by registering an onchange handler to the select element:
document.getElementById("theSelect").onchange = function() {
var len = this.options.length;
var selected = [];
for(var i = 0; i < len; i++) {
if(this.options[i].selected) {
selected.push(this.options[i].value);
}
}
alert(selected);
//so you can do something like getUsers(selected.join(","));
};
Working example.
It's the same. The difference is that when you send data to server, you send a list of name. Then you can use php in server to build the HTML and return it.
if ur using checkboxes try using POST method instead of GET
xmlhttp.open("POST","getuser.php",true);
<input tabindex="3" type="checkbox" name="gender[]" id="gender" value="M">Male
<input tabindex="4" type="checkbox" name="gender[]" id="gender" value="F">Female
try print_r($_POST);
Thanks Sanil
精彩评论