问题
I have a simple page where I can select a client, then once I chose that autopopulate to the projects that belong to the client. I am using PHP/MySQL to pull the results.
I took at a look at this: http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/ but I think that starts with both fields on the page. I tried to rework the code but didn't come out that well.
var client_id = $('#c_id').val();
$.getJSON("../inc/get-projects.php", {id: client_id}, function(data){
projects = $('#p_id');
projects.empty();
$.each(data, function() {
var option = $('<option/>').attr('value', this.id).text(this.name);
projects.append(option);
});
});
PHP:
<?php
include "config.inc.php";
$sth = mysql_query(
sprintf(
"SELECT c_id,p_id,p_title FROM projects WHERE c_id = %s",
mysql_real_escape_string($_GET['id'])
)
);
$projects = array();
while($r = mysql_fetch_assoc($sth)) {
$projects[] = array('id' => $r['p_id'], 'name' => $r['p_title']);
}
print json_encode($projects);
exit;
?>
回答1:
If you have HTML like this:
<select id='clients'>...</select>
<select id='projects'>...</select>
You can have jQuery code like this:
$(document).ready(function() {
var $clients = $('#clients');
$clients.change(function() {
var client_id = $clients.val();
$.getJSON("getProjects.php", {id: client_id}, function(projects) {
$projects = $('#projects');
$projects.empty();
$.each(projects, function() {
var option = $('<option/>').attr('value', this.id).text(this.name);
$projects.append(option);
});
});
});
});
And then have getProjects.php return something like:
$sth = mysql_query(
sprintf(
"SELECT * FROM projects WHERE client_id = %s",
mysql_real_escape_string($_GET['id'])
)
);
$projects = array();
while($r = mysql_fetch_assoc($sth)) {
$projects[] = array('id' => $r['id'], 'name' => $r['name']);
}
print json_encode($projects);
exit;
I haven't tested all of this, but it is more or less what you want, I think.
edit - Tested and works, obviously it might not be exactly what you need but it gives you an idea of how to go about it. Hope it helps.
edit 2 - The problem with your code is here:
$.getJSON("../inc/get-projects.php", {id: client_id}, function(projects){
projects = $('#p_id');
projects.empty();
$.each(projects, function() {
var option = $('<option/>').attr('value', this.id).text(this.name);
projects.append(option);
});
});
You are overwriting the projects
passed into the function with the projects
in the 2nd line. I used a $
in my code to differentiate the two, which was admittedly perhaps not the best of ways. To fix, change the code to this:
$.getJSON("../inc/get-projects.php", {id: client_id}, function(data){
projects = $('#p_id');
projects.empty();
$.each(data, function() {
var option = $('<option/>').attr('value', this.id).text(this.name);
projects.append(option);
});
});
回答2:
The answers to this question might prove helpful: Using javascript and jquery, to populate related select boxes with array structure
来源:https://stackoverflow.com/questions/548540/jquery-and-auto-populat-selects