Hi all,
I posted up a thread a few weeks ago, regarding automatically updating a drop down list from a database using ajax.
Someone posted a reply suggesting i use the w3schools website for some help and i managed to put together something that worked for me.
However, i'm now coming across a problem where i need to use the ajax feature more than once on the same page.
I'll try post a link to give a sort of working example and i'll also try explain myself.
At the moment i have 4 select boxes:
Category
Sub Category
Manufacturer
Model
What happens, if i select a value from the Category list, the model list gets updated.
But what should happen is, Category updates sub category and then manufacturer updates model.
If i choose a value from manufacturer first, the model list updates correctly.
So as you can see it's kinda half working.
What i've been doing is calling two different .js files and using 2 different onChange events, hoping that it would update how i wanted it to.
<select name="cat_id" class="input" id="cat_id" onchange="subcatUpdate(this.value)">
<select name="man_id" class="input" id="man_id" onchange="modelUpdate(this.value)">
Below is a copy of my 2 .js files
var xmlhttp;
function subcatUpdate(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="update_subcat.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("subcat_id").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
var xmlhttp;
function modelUpdate(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="update_model.php";
url=url+"?m="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("happy_id").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
And finally my HTML/PHP Code
<table width="54%" cellpadding="5" cellspacing="5" class="table_style">
<tr>
<td width="21%"><label>Category:<br />
<select name="cat_id" class="input" id="cat_id" onchange="subcatUpdate(this.value)">
<option value="Please Select">Please Select</option>
<?php
// Open Connection to DB
require_once ('../include/config.inc_admin.php');
require_once (MYSQL);
$q = "SELECT cat_id, cat_name FROM categories";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo "<option value=" . $row['cat_id'] . ">" . $row['cat_name'] . "</option>";
}
?>
</select></label></td>
</tr>
<tr>
<td><label>Sub Category:<br />
</label><div id="subcat_id"></div></td>
</tr>
<tr>
<td><label>Manufacturer:<br />
<select name="man_id" class="input" id="man_id" onchange="modelUpdate(this.value)">
<option value="Please Select">Please Select</option>
<?php
$q = "SELECT man_id, manufacturer FROM manufacturers";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$man_id = $row['man_id'];
echo "<option value=" . $row['man_id'] . ">" . $row['manufacturer'] . "</option>";
}
?>
</select>
</label></td>
</tr>
<tr>
<td><label>Model:<br />
</label><div id="happy_id"></div></td>
</tr>
I've left out my php files for connecting to the database as i know these are functioning okay.
Is the method i'm usin the correct way to be going about it or am i missing something out which is causing me to have this problem?
Any help is greatly appreciated,
Regards,
Dan