Hi, I am learning how to use AJAX; I have the following code for populating select tags:
<html>
<head>
<title>My Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getSpan(Frame) {
var strURL="findSpan.php?Frame="+Frame;
var req = getXMLHTTP();
if (req) {
req.onreadySpanchange = function() {
if (req.readySpan == 4) {
if (req.status == 200) {
document.getElementById('Spandiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getModel(Frame,Span) {
var strURL="findModel.php?Frame="+Frame+"&Span="+Span;
var req = getXMLHTTP();
if (req) {
req.onreadySpanchange = function() {
if (req.readySpan == 4) {
if (req.status == 200) {
document.getElementById('Modeldiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">Frame</td>
<td width="150"><select name="Frame" onChange="getSpan(this.value)">
<option value="">Select Frame</option>
<option value="-1">FS</option>
<option value="0">LT</option>
</select></td>
</tr>
<tr style="">
<td>Span</td>
<td ><div id="Spandiv"><select name="Span" >
<option>Select Span</option>
</select></div></td>
</tr>
<tr style="">
<td>Model</td>
<td ><div id="Modeldiv"><select name="Model">
<option>Select Model</option>
</select></div></td>
</tr>
</table>
</form>
</body>
</html>
populate Span tag
<? $Frame=intval($_GET['Frame']);
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db1');
$query="SELECT span FROM bays where fs=$Frame group by span order by span";
$result=mysql_query($query);
?>
<select name="Span" onchange="getModel(<?=$Frame?>,this.value)">
<option>Select Span</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['span']?>><?=$row['span']?></option>
<? } ?>
</select>
populate Model tag
<? $Frame=intval($_GET['Frame']);
$Span=intval($_GET['Span']);
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db1');
$query="SELECT qbay.type as type, qbay.load as myload, qbay.bprice/(qbay.length*qbay.span) as myprice, qbay.length as width FROM qbay where qbay.span=$Span and qbay.fs=$Frame order by qbay.load, qbay.type";
$result=mysql_query($query);
?>
<select name="Model">
<option>Select Model</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['type']?>><?=$row['type']?></option>
<? } ?>
</select>
I cannot get it working - tags Span and Model are empty. I guess the problem is with passing parameters/values but don't know what exactly. Any idea what wrong in this code please?
<html>
<head>
<title>My Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
function getXMLHTTP() {
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getSpan(Frame) {
var strURL="findSpan.php?Frame="+Frame;
var req = getXMLHTTP();
if (req) {
req.onreadySpanchange = function() {
if (req.readySpan == 4) {
if (req.status == 200) {
document.getElementById('Spandiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getModel(Frame,Span) {
var strURL="findModel.php?Frame="+Frame+"&Span="+Span;
var req = getXMLHTTP();
if (req) {
req.onreadySpanchange = function() {
if (req.readySpan == 4) {
if (req.status == 200) {
document.getElementById('Modeldiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">Frame</td>
<td width="150"><select name="Frame" onChange="getSpan(this.value)">
<option value="">Select Frame</option>
<option value="-1">FS</option>
<option value="0">LT</option>
</select></td>
</tr>
<tr style="">
<td>Span</td>
<td ><div id="Spandiv"><select name="Span" >
<option>Select Span</option>
</select></div></td>
</tr>
<tr style="">
<td>Model</td>
<td ><div id="Modeldiv"><select name="Model">
<option>Select Model</option>
</select></div></td>
</tr>
</table>
</form>
</body>
</html>
populate Span tag
<? $Frame=intval($_GET['Frame']);
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db1');
$query="SELECT span FROM bays where fs=$Frame group by span order by span";
$result=mysql_query($query);
?>
<select name="Span" onchange="getModel(<?=$Frame?>,this.value)">
<option>Select Span</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['span']?>><?=$row['span']?></option>
<? } ?>
</select>
populate Model tag
<? $Frame=intval($_GET['Frame']);
$Span=intval($_GET['Span']);
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db1');
$query="SELECT qbay.type as type, qbay.load as myload, qbay.bprice/(qbay.length*qbay.span) as myprice, qbay.length as width FROM qbay where qbay.span=$Span and qbay.fs=$Frame order by qbay.load, qbay.type";
$result=mysql_query($query);
?>
<select name="Model">
<option>Select Model</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['type']?>><?=$row['type']?></option>
<? } ?>
</select>
I cannot get it working - tags Span and Model are empty. I guess the problem is with passing parameters/values but don't know what exactly. Any idea what wrong in this code please?