0

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

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by cmccully
0

but when you do the case, did you use swtich ()? put all code please... you maybe forget to write onChange in the txt input...

and you have something wrong... your code:

case 'addCharacteristics':
        { // THIS DONT GO !!! IS SWTICH($VAR){CASE 'lala': code break;}
        // 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;
        }

You might write:

switch($yout_var){
     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;
}

Edited by P0lT10n: n/a

0

Thank you for your response P0lT10n,


The case I included is indeed part of a larger larger switch block. I only included the relevant case. I removed the extraneous brackets around the case elements as you suggested but with no effect.

The switch block is functioning in that it does prevent the record from being added to the database. The part I am trying to resolve is where it echos back to the AJAX script.

$sql = "SELECT * FROM characteristic WHERE characteristic = '$cleanTxtAddChar'";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 )
     {
     echo "dup";
     break;
     }

Is there a way I can see what is coming back from the server to the AJAX script? I have tried to setup different alert boxes to read xmlhttp.responseText but I am not getting anything to display.

Thanks for your help any suggestions are appreciated.

cmccully

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.