Forum Moderators: open
This code works, but it shows the information on both div tags on my page, even though they are named different.
Can anybody tell me what the problem is?
var xmlHttpfunction hvacUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser.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 ¦¦ xmlHttp.readyState=="complete")
{
document.getElementById("hvacHint").innerHTML=xmlHttp.responseText
}
}
function plumbingUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getplumbing.php"
url=url+"?p="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChange()
{
if (xmlHttp.readyState==4 ¦¦ xmlHttp.readyState=="complete")
{
document.getElementById("plumbingHint").innerHTML=xmlHttp.responseText
}
}
Here is the form
<form name="editform" action="#" method="POST">
<select name="plumbing" id="plumbing" onClick="plumbingUser(this.value)">
<option></option> <?php
$sql="SELECT * FROM contractors WHERE type = 'Plumbing' ORDER BY name ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option><br />
<?php
}
?>
</select>
</td>
</tr>
</table></td>
</tr>
<tr>
<td>HVAC Contractor</td>
<td><table width="600" cellpadding="5" cellspacing="0">
<tr>
<td width="200">
<div id="hvacHint"><b><?php echo $row9['hvac_contractor']; ?></b></div></td>
<td width="200">
<select name="hvac" id="hvac" onClick="hvacUser(this.value)">
<option>--</option>
<option value="an">Add New</option>
<option>--</option>
<?php
$sql="SELECT * FROM contractors WHERE type = 'HVAC' ORDER BY name ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{ ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></option><br />
<?php
}
?>
</select>
</form>
In the header, here is ajax.js
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
} Then the selectuser.js
var xmlHttpfunction hvacUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser.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 ¦¦ xmlHttp.readyState=="complete")
{
document.getElementById("hvacHint").innerHTML=xmlHttp.responseText
}
}
function plumbingUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getplumbing.php"
url=url+"?p="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChange()
{
if (xmlHttp.readyState==4 ¦¦ xmlHttp.readyState=="complete")
{
document.getElementById("plumbingHint").innerHTML=xmlHttp.responseText
}
}
xmlHttp.onreadystatechange=function(req){
return function() {
if (req.readyState==4 ¦¦ req.readyState=="complete") {
document.getElementById("hvacHint").innerHTML=req.responseText
}
}
}(xmlHttp);
In other words, you assign an anonymous function as your callback, and that function can access the xmlHttp object (as parameter "req").