PDA

View Full Version : AJAX responseText not getting reading server response



cmccully
22 Sep 2010, 10:21 PM
Hi all


I have an AJAX script that reads the contents of a textbox and sends the value to a php script to be inserted into a database. If the record already exists in the db then the the php script does not insert the record and sends a message back to the AJAX script to notify the user.

I have most of it working correctly, the only issue is in reading the AJAX responseText when there is a duplicate entry.

php issues a simple echo statement when there is a duplication entry:

echo "duplicate";
I have manually entered the query string in the browser and php writes the word duplicate to the screen when appropriate so I think the server side of the equation is working correctly.

Here is my form:

<form id="frmAddChar" name="frmAddChar" method="get" action="" onsubmit="addChar();">
<h3>Add A Characteristic</h3>
<p>&nbsp;</p>
<p><input name="txtAddChar" type="text" id="txtAddChar" size="30" maxlength="250" /></p>
<p>&nbsp; </p>
<input type="submit" name="Submit" value="Submit" />
<input type="hidden" name="selection" value="addCharacteristics"/>
</form>
Here is the function it calls:

function addChar()
{
var characteristic = document.getElementById("txtAddChar").value;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (xmlhttp.responseText == "duplicate")
{
alert ("Duplicate Entry!");
}
}
}
xmlhttp.open("GET","ctrPanelProc.php?selection=addCharacteristics&txtAddChar="+characteristic,true);
xmlhttp.send();
}
As I have said the function correctly sends the textbox entry to the php script but it completely ignores php's error response. Any suggestions as to what I'm doing wrong here? Any help is appreciated. Thanks!


cmccully