I have three listboxes on a webpage. I would like to save the value from the first listbox so that I can populate the second. I can't chain the first to the second because of the problem of duplicates. Its a many to many relationship. How would I save the value temporarly so that I can use it to populate the second listbox. Thank you.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 0);
include("conndb.php");
function createoptions($table , $id , $field , $condition_field , $value)
{
$sql = sprintf("select * from $table WHERE $condition_field=%id ORDER BY $field" , $value);
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) > 0) {
while ($a = mysql_fetch_assoc($res))
$out[] = "{optionValue: {$a[$id]}, optionDisplay: '$a[$field]'}";
return "[" . implode("," , $out) . "]";
} else
return "[{optionValue: -1 , optionDisplay: 'No result'}]";
}
if (isset($_GET['make'])) {
echo createoptions("model" , "model_id" , "model" , "make_id" , $_GET['make']);
}
die();
This is the other page...
<?Php
include("conndb.php");
function createoptions($table , $id , $field)
{
$sql = "select * from $table ORDER BY $field";
$res = mysql_query($sql) or die(mysql_error());
while ($a = mysql_fetch_assoc($res))
echo "<option value=\"{$a[$id]}\">$a[$field]</option>";
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Select test</title>
<script type="text/javascript" src="jquery-1.2.3.js"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
$("select#make").change(function(){
$.getJSON("select.php",{make: $(this).val(), ajax: 'true'}, function(j){
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
}
$("select#model").html(options);
})
})
})
</script>
</head>
<select id="make">
<option value="-1">--Select--</option>
<?php
createoptions("make", "make_id", "make");
?>
</select>
<select id="model">
</select>
</body>
</html>
And I am using jquery-1.2.3.js