Hi all,
I have an AJAX function that sends the contents of a text field to a PHP file. The file checks for duplicate entries and if unique stores the new record in MYSQL. This all works fine. However, I would like the PHP file to send back an alert that the record is a duplicate and was not entered.
Here is the JavaScript
function addChar()
{
var characteristic = "";
characteristic = document.getElementById("txtAddChar").value;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
if (xmlhttp.responseText == "dup")
{
alert ("Duplicate Entry!");
}
}
}
xmlhttp.open("GET","ctrPanelProc.php?selection=addCharacteristics&txtAddChar="+characteristic,true);
xmlhttp.send();
}
Here is the php:
case 'addCharacteristics':
{
// Look for duplicate entry
$sql = "SELECT * FROM characteristic WHERE characteristic = '$cleanTxtAddChar'";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 )
{
echo "dup";
break;
}
// Insert entry into DB
$sql = "INSERT INTO characteristic (characteristic) VALUES ('$cleanTxtAddChar')";
mysql_query($sql);
break;
}
I had assumed that I could read the responseText method and determine if there was a problem but it is not working. Any ideas as to why I am not getting anything back when I have a duplicate entry? Thanks!
cmccully