Ajax and php : XMLHttpRequest « Ajax Layer « JavaScript DHTML






Ajax and php

 
<html>
<head>
<title></title>
</head>
<body>
<div id="xmldata"></div>
<script type="text/javascript">
function readyAJAX() {
try {
    return new XMLHttpRequest();
} catch(e) {
    try {
        return new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
        try {
            return new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            return "A newer browser is needed.";
        }
    }
}
}
var requestObj = readyAJAX();
var url ="http://127.0.0.1/sum.php?num1=2&num2=2";
requestObj.open("GET",url,true);
requestObj.send();
requestObj.onreadystatechange = function() {
    if (requestObj.readyState == 4) {
        if (requestObj.status == 200) {
            document.write(requestObj.responseText);
        } else {
            document.write(requestObj.statusText);
        }
    }
}
</script>
</body>
</html>


//sum.php

<?php
print $_GET['num1'] + $_GET['num2'];
?>

   
  








Related examples in the same category

1.Is your Browser Ajax ready
2.Use XMLHttpRequest to send and read data