The technique used could equally create tables or any other HTML on the fly, set session information, cart items whatever.
You basically require 3 files (in this case all in the same directory but you could always add paths to the code)...
index.php (name not important but your "caller" php).
sublist.php (again name is up to you but need to change the code to reflect any change).
prototype.js (loaded from here
The file contents (Will not go into prototype javascript library).
index.php
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="prototype.js" type="text/javascript"></script>
<script type="text/javascript">
function getSecond(value) {
var url = 'sublist.php';
var myAjax = new Ajax.Request
(
url,
{
method: "post",
parameters : "first="+value,
onSuccess: function transResult (response) {
document.getElementById('sublist').innerHTML=response.responseText;
},
onFailure: function transResult (response) {
alert ('Failure'+response.responseText);
}
}
);
return false;
}
</script>
</head>
<body>
<span id="firstlist">
<select name="first" onchange="getSecond(this.value);">
<option value="1">Names starting with A</option>
<option value="2">Names starting with B</option>
<option value="3">Names starting with C</option>
<option value="4">Names starting with D</option>
<option value="5">Names starting with E</option>
</select>
</span>
<span id="sublist"><?php include 'sublist.php'; ?></span>
</body>
</html>Code: Select all
<?php
// Define the result list possibilities. This could easily be retrieved from a database
$sublist=array(1=>array('Anna','Andrew','Andrea','Annakin'),
2=>array('Bill','Bob','Bernie'),
3=>array('Charles','Camila','Connie','Constance'),
4=>array('Daniel','Darla','Danny'),
5=>array('Edmund','Edgar','Elisabeth','Eugene','Emma','Emily'));
// Process
$value=1;
if (!empty($_POST['first'])) {
if (isset($sublist[$_POST['first']])) $value=$_POST['first'];
}
$name_list='';
foreach ($sublist[$value] as $name) {
$name_list.="<option value=\"{$name}\">{$name}</option>\n";
}
echo '<select name="second">'.$name_list.'</select>';
?>