If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > AJAX Nested If Statement Error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-10, 22:28
tlshaheen tlshaheen is offline
Registered User
 
Join Date: Dec 2009
Posts: 20
AJAX Nested If Statement Error

I know this is a PHP forum, but thought some of you may be able to offer help with the Javascript portion as well.
I'm running Ajax validation on a form I have, and the following is my javascript file. It runs fine, except for the statechangedFname portion. You can see its different from the other statechanged, as I added a nested if statement:
Code:
	  	  if (xmlhttp.status == 200) {
This is standard in Ajax, to see if the response is "ok." For some reason, the javascript stops reading once it gets to a nested if statement inside my functions! It doesn't matter if I have

Code:
var test=1
if (test==1) {
which is obviously true, it stops reading. If I have any code below (not in) the if statement, those are not read either.
I don't understand why, as it should work, correct?

The Javascript code is taken from a w3 demo, I edited it a bit to fit my needs.

Thanks!


Code:
// JavaScript Document
var xmlhttp;

function checkAjaxAlpha(str , id)
{
	str = encodeURIComponent(str);
if (str.length==0)
  {
	  //Check ID, output to correct Document ID
	  
	  if (id=="Fname") {
	document.getElementById("alpha_check_fname").innerHTML="";
	  }
	  if (id=="Lname") {
	document.getElementById("alpha_check_lname").innerHTML="";
	  }
	  if (id=="City") {
	document.getElementById("alpha_check_city").innerHTML="";
	  }
  	return;
  }
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
var url="../users/form_checks/alpha.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();

	  if (id=="Fname") {
		xmlhttp.onreadystatechange=stateChangedFname;
	  }
	  if (id=="Lname") {
		xmlhttp.onreadystatechange=stateChangedLname;
	  }
	  if (id=="City") {
		xmlhttp.onreadystatechange=stateChangedCity;
	  }
xmlhttp.open("GET",url,true);

xmlhttp.send(null);


}

function stateChangedFname()
{
if (xmlhttp.readyState==4)
  {
	  	  if (xmlhttp.status == 200) {
			  document.getElementById("alpha_check_fname").innerHTML=xmlhttp.responseText;
		  }
  }

}

function stateChangedLname()
{
if (xmlhttp.readyState==4)
  {
	  if(xmlHttpRequest.status==200){

  		document.getElementById("alpha_check_lname").innerHTML=xmlhttp.responseText;
	  }
  }
}

function stateChangedCity()
{
	if (xmlhttp.readyState==4)
  	{
		  document.getElementById("alpha_check_city").innerHTML=xmlhttp.responseText;
  	}
}


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On