Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey! this is my code , i am very new to ajax and i was just following some tutorial , but when i completed the code , this error came on developer tools

Uncaught TypeError: Cannot read property 'DocumentElement' of null on line 45

i don't get this, can someone plzz solve my problem , i've asked this question on 3-4 places but noone's answering , please help
var xmlHttp = createXmlHttpRequestObject();          //xmlHttp --> a main object in Ajax 

function createXmlHttpRequestObject(){
	var xmlHttp;
	
	if(window.ActiveXObject){                        	//when user not using IE	
		try{				
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){			
				xmlHttp = false;
			}
    }		
	else{	
		try{				
				xmlHttp = new XMLHttpRequest();
		}catch(e){			
				xmlHttp = false;
		}			
	}
    
	if(!xmlHttp){		
		alert("Errorr yrrrrrr!!");
	}
	else{		
		return xmlHttp;                                 //because in the first line , it is equal to the function so needs to return
	}
}

function process(){							                   //takes that Communication object(xmlHttp) and sends request to the server
	if(xmlHttp.readyState==0 ||  xmlHttp.readyState==4){		
		name = encodeURIComponent(document.getElementById('mname').value);                    
		xmlHttp.open("GET", "muslims.php?name="+name, true);             //if it should be handled asyncrounsly 
		xmlHttp.onreadystatechange = handleServerResponse;      //this handles the server response after you send request to server
		xmlHttp.send(null);																	
	}
	else{		
		setTimeout('process()',1000);
	}
}

function handleServerResponse(){                           //sends back xml code in php file
	if(xmlHttp.readyState==4){		
		if(xmlHttp.status==200){						   //200 means communication went normal			
			xmlResponse = xmlHttp.responseXML;             //basically xmlResponse is now your xml from php file
			xmlDocumentElement = xmlResponse.DocumentElement;
			message = xmlDocumentElement.FirstChild.data;        //this message will be equal to one of the echos from php file
			document.getElementById('show').innerHtml = '<span style="color:red">'+ message + '</span>';
			setTimeout('process()',1000);
		}
		else{	
		alert("Error in the end yarrrrrrr!!!");
		}
	}
    }
Posted
Updated 2-Sep-13 22:16pm
v4
Comments
Ron Beyer 3-Sep-13 0:35am    
Since we can't see what the line numbers are, can you point out line 45 for us please?
Member 10230817 3-Sep-13 1:22am    
xmlDocumentElement = xmlResponse.DocumentElement; this is the line

The XML returned by your php file might not be correct and causing issue. Please check below link

responseXML.documentElement returns null object[^]
 
Share this answer
 
Comments
Member 10230817 3-Sep-13 9:09am    
this is my php file , i don't see any error , i tried changing little things but no luck :(

<!--?php header('Content-type: text/xml');
echo ''

echo '<response>'
$name = $_GET['name'];
$namearray = array('Muhammad', 'Ali' , 'Fatima', 'Hassan', 'Hussain');
if(in_array($name,$namearray)){
echo "Yay!!!! It is a muslim name , whatever";
}
else if($name==""){
echo "You have to enter some name in the field you dumbass";
}
else{
echo "Either your name is not muslim or we dont have this name, please dobara hath pair marain";
}
echo ''
?>
Mahesh Bailwal 3-Sep-13 10:08am    
can you please share output XML of this php page which browser is reeving through AJAX call.
Don't go for conventional Ajax request.Try jquery ajax to accomplish this task.

include jquery.js library in your project from JQuery site
Then reference it on your used page
Now make an ajax call as below

$(document).ready(function(){
   $.ajax(function(){
     url:"signupin.php",
     mType:"GET",
     async:"true",
     success:function(){
       alert("Hello");
     }
   });
});


For reference please have a look at jquery documentation from below link
ajax call using JQuery
See the magic!Hope this will help you.
 
Share this answer
 
Comments
Member 10230817 3-Sep-13 11:07am    
it's kinda embarrassing that i am asking you this but which code should i replace this with? :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900