Can I ask an AJAX question here?
I realized after thinking for a moment that it is the PHP part of the AJAX function that is the problem, so I'll ask the question.
Sorry for the long HTML code. I figured if I only post the last part someone would wonder what was happening above it.
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.agentname.value=xmlHttp.responseText;
}
}
xmlHttp.open("POST","getAgent.php",true);
xmlHttp.send(null);
}
</script>
<form name="myForm">
Agent ID: <input type="int"
onkeyup="ajaxFunction();" name="agent_id" id="agent_id" />
<input type="text" name="agentname" />
</form>
</body>
</html>
I commented out lines 4-7 to make sure that wasn't the problem and to pose a second question: Is there an easier way to create this variable?
I don't think an If/Else statement is needed, and may actually cause a problem when being returned by the AJAX function.
<?php include("Connections/custsupadmin.php"); ?>
<?php
$agentID = mysql_real_escape_string($_POST['agent_id']
//Should the above variable work just as well as the variable below??
//$agentID = "";
//if (isset($_POST['agent_id'];
// $agentID = mysql_real_escape_string($_POST['agent_id']);
//else die();
$getAgent = mysql_query("SELECT *
FROM agents
WHERE agent_id = '" . $agentID . "'");
while($row = mysql_fetch_array($getAgent))
{
echo $row['first_name'] ." ". $row['last_name'];
}
mysql_close()
?>
The problem is when you enter an agent_id in the form on the html page it sends the request to the server but results in
<br />
If I assign a value to " . $agentID . " and just run the script it works. Any ideas?