Adnan Zahid
Adnan Zahid

Reputation: 583

Simple AJAX example for PHP not working

This is my first question regarding PHP so please bear with me. I've been following tutorials from phpacademy.org.

I'm stuck on one tutorial where an intro to AJAX is given. I typed in the exact code as the tutor, still its not working on my end.

I searched alot for it but it didnt help one bit. Could anybody help me out here? Here's my code:

<html>
<head>
<script type="text/javascript">
function load(){

if(window.XMLHttpRequest)
    xmlhttp=new XMLHttpRequest();
else
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');

xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementsById('adiv').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'AJAX.inc.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="adiv"></div>
<input type="submit" value="Submit" onclick="load();">
</body>
</html>

And here's the AJAX.inc.php file:

<?php
echo 'Hello AJAX';
?>

However another example from w3school.com is working.

Possible duplicate: AJAX not working with XAMPP or is it just impossible

But this question isn't properly answered (or I dont understand it). Would someone please clarify it?

Upvotes: 0

Views: 1462

Answers (2)

Naga Raju
Naga Raju

Reputation: 11

<html>
<head>
<script type="text/javascript">
function load(){

if(window.XMLHttpRequest)
    {
    xmlhttp=new XMLHttpRequest();
    }
else
    {
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
    }
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById('adiv').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'ajax_php.php', true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="adiv"></div>
<input type="submit" value="Submit" onclick="load();">
</body>
</html>

Upvotes: 1

xdazz
xdazz

Reputation: 160923

There is no document.getElementsById method.

document.getElementsById('adiv').innerHTML=xmlhttp.responseText;

Should be

document.getElementById('adiv').innerHTML=xmlhttp.responseText;

Upvotes: 2

Related Questions