PHP with Get Method-2
Send request to the server and reterive data from server without page refresh

In this section we are sending request to the server then getting data from server by get method. in this section we create three page

1. HTML
2. Javascript
3. PHP

Html Page

<html>
<head>
<script language="javascript" type='text/javascript' src='ajax.js'></script>
<title>simple Ajax Example</title>
</head>
<body>
<input type="submit" onclick="getvalue('php_page.php?value=1');" value="Buttom 1"/>
<input type="submit" onclick="getvalue('php_page.php?value=2');" value="Buttom 2"/>
<div id='txt'>
This is my div text.
</div>
</body>
</html>

Javascript page

// JavaScript Document
function getvalue(datasource)
{
var XMLHttpRequestObject=false;

if(window.XMLHttpRequest)
{
XMLHttpRequestObject=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
}

XMLHttpRequestObject.onreadystatechange = function()
{
if(XMLHttpRequestObject.readyState == 4)
{
document.getElementById('txt').innerHTML = XMLHttpRequestObject.responseText;
}
}

XMLHttpRequestObject.open("GET",datasource, true);
XMLHttpRequestObject.send(null);
}

PHP page

<?php
if($_REQUEST['value']==1)
{
    echo "You have clicked on Buttom1";
}

if($_REQUEST['value']==2)
{
    echo "You have clicked on Buttom2";
}
?>


Previous
Next