[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

PHP AJAX duplicate check

With the code included, I am trying to detect if the entered customer id already exists in the database. If it does, I want to display an error message.  This process is working fine through the CheckForDuplicateCustomerStateChanged function. This function is getting the XML from the PHP page. I have verified that the correct brach of the IF is executing by putting alert statements the different branches. The problem is, when control returns back to the SaveCustomer functio (after the call to CheckForDuplicateCustomer), the value of the variable isUnique is always false.

What am I doing wrong?
var isUnique = new Boolean(false);
 
function SaveCustomer()
{
    txtCustomerId = document.getElementsByName("txtcustomerid")[0];
    isUnique = false;
    if (txtCustomerId.value == "")
    {
      errorText = document.getElementById("idError");
      document.getElementById("idError").innerHTML = "The Customer Id is required.";
      errorText.style.visibility="visible";
    }
    else
    {
      custPk = document.getElementsByName("txtid")[0].value;
      CheckForDuplicateCustomerId(txtCustomerId.value, custPk)
      if (!isUnique) 
      {
        errorText = document.getElementById("idError");
        document.getElementById("idError").innerHTML = "The Customer Id is not unique.";
        errorText.style.visibility="visible";
      }
      else
      {
        document.editcustomerform.submit()
      }
    }
}
 
function CheckForDuplicateCustomerId(custId, custPk)
{
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null)
  {
    alert ("Your browser does not support HTTP Request");
    return;
  }
  var url="getcustomerbycustid.php";
  url=url+"?customerid="+custId+"&custpk="+custPk;
  xmlHttp.onreadystatechange=CheckForDuplicateCustomerIdStateChanged; 
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
 
function CheckForDuplicateCustomerIdStateChanged()
{
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
  {
    var responseValue = parseInt(xmlHttp.responseXML.text);
    if (responseValue == -1)
    {
      alert("An error occured while checking the customer id. Please try again.");
    }
    else if (responseValue == 0)
    {
      isUnique = true;
    }
    else
    {
      isUnique = false;
    }
  }
}

Open in new window

0
PIComponents
Asked:
PIComponents
1 Solution
 
LordOfPortsCommented:
It is highly likely that CheckForDuplicateCustomerId is done before the asynchronous call to getcustomerbycustid.php is finished and therefore the value of isUnique does not get changed on time.

To test the theory try passing "false" as the third argument to the open function, i.e. line 41 in the code snippet above should be:
xmlHttp.open("GET", url, false); 
// The call should not be asynchronous in this case because subsequent code
//  depends on the returned value so you need to wait for it to finish.

Open in new window

0
 
PIComponentsAuthor Commented:
BINGO! That's what I was looking for!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now