Hello all,
I am just learning AJAX because I find it interesting but my first example does not work the way I planned it.
Here is the code:
This is my HTML file named ajax.html

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="" />

    <title>Untitled 3</title>
    <script type="text/javascript" language="javascript" src="jscript.js">

</script>

</head>

<body>

<h1> My AJAX example</h1>
<button onclick="callAjax()" >Ajax</button>
<div id="d" name="d"></div>
</body>
</html>

// This is my javascript file named jscript.js

function callAjax(){
    alert("ajax method called");
    if (window.active)
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
    if(xhr.readyState==4 && xhr.status==200){
    document.getElementById("d").innerHTML=xhr.responseText;
}
};
xhr.open('GET','ajax.php',true);
xhr.send();

}

// This is my server side PHP code named ajax.php
<?php
echo "Welcome to AJAX world";

?>

Recommended Answers

All 4 Replies

You should provide a full URL, not just ajax.php in the xhr.open()

Next time, please use code tags.

It looks like you are missing a { after if(window.active)

I'd recommend: http://www.w3schools.com/ajax for setting your initial structure up and then modifying for your needs from there.

Thank you all for showing concern. My codes later worked after adjusting some things.

Great!

- please upvote anyone who helped.
- please mark thread as solved.
- please post specifics about solution if possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.