Ok so I have two separate AJAX function that I want to combine but have no idea how. The first 2 files are to call up a table with cells in the form of text-boxes, and the last 2 files are linked dropboxes.
What I want to do is to call the table when the value of the 'company' dropbox is changed based on the '$val' of that selection.
Please read the codes to get a better idea:
testform.php
<html>
<head>
</head>
<body>
<form>
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
[B]//these will return the query only for company with company_id 1-4[/B]
</form>
<br />
<div id="[B]txtHint[/B]"><b>Company info will be listed here.</b></div>
</body>
</html>
<script language=Javascript>
var xmlhttp;
function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="[B]getuser.php[/B]";
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("[B]txtHint[/B]").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
</script>
getuser.php
<?php
$q=$_GET["q"];
include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/config.php');
include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/opendb.php');
$sql="SELECT * FROM location WHERE company_id = '".$q."'";
$result = mysql_query($sql);
echo "<table>
<tr>
<th>Name</th>
<th>State</th>
<th>Block</th>
<th>Street</th>
<th>Building</th>
<th>Unit</th>
<th>Postal</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input type=\"text\" name=\"locname\" size=\"10\" value=\"" . $row['name'] .
"\"></td>";
echo "<td><input type=\"text\" name=\"locname\" size=\"10\" value=\"" . $row['state'] .
"\"></td>";
echo "<td><input type=\"text\" name=\"locname\" size=\"6\" value=\"" . $row['blk_no'] .
"\"></td>";
echo "<td><input type=\"text\" name=\"locname\" size=\"50\" value=\"" . $row['street'] .
"\"></td>";
echo "<td><input type=\"text\" name=\"locname\" size=\"15\" value=\"" . $row['building']
. "\"></td>";
echo "<td><input type=\"text\" name=\"locname\" size=\"6\" value=\"" . $row['unit_no'] .
"\"></td>";
echo "<td><input type=\"text\" name=\"locname\" size=\"6\" value=\"" . $row['postal'] .
"\"></td>";
echo "</tr>";
}
echo "</table>";
include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/closedb.php');
?>
actualform.php (this is how I want my form to look like)
<html>
<head>
</head>
<body>
<form>
<table align="center">
<tr><td align="left"><b>Country:</b></td>
<td><font id=country><select>
<option value='0'>No Value</option></td>
<tr><td align="left"><b>Company:</b></td>
<td><font id=company><select onchange="[B]showuser(this.value)[/B]">
<option value='0'>No Value</option></select></td>
</tr>
</table>
</form>
<br />
<div id="[B]txtHint[/B]"></div>
</body>
</html>
<script language=Javascript>
//I didn't include the "showuser" function here coz it's not working anw
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
try { return new XMLHttpRequest(); } catch(e) {}
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText;
}
}
};
req.open("GET", "[B]dropdown.php[/B]?data="+src+"&val="+val);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-
1");
req.send(null);
}
window.onLoad=dochange('country', -1);
window.onLoad=dochange('company', -1);
</script>
dropdown.php
<?
$data=$_GET['data'];
$val=$_GET['val'];
include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/config.php');
include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/opendb.php');
if ($data=='country') {
echo "<select name='country'
onChange=\"dochange('company', this.value)\">\n";
echo "<option value='0'>======Select======</option>\n";
$result=mysql_query("SELECT country_id, name
FROM country
ORDER BY name")
or exit (mysql_error());
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
} else if ($data=='company') {
echo "<select name='company'>\n";
echo "<option value='0'>======Select======</option>\n";
$result=mysql_query("SELECT c.company_id, c.name
FROM company c, country co
WHERE c.country_id = co.country_id
AND c.country_id = '$val'
ORDER BY c.name")
or exit (mysql_error());
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
echo "</select>\n";
include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/closedb.php');
?>