pTutorial

New way to learn Programming

Ajax Example In PHP

Print Table using Ajax

Following program print the table of any given number with the help of Ajax without reloading the whole page. The request of the index.html will catch by the argument1.php and response will be return and set to the kk id as previous example.

Ajax table example

<!--index.html-->

<html>
<head>
<script>
var obj;
function printTable()
{
var u=document.frm.num.value;
var url="argument1.php?val="+u;

if(window.XMLHttpRequest){
obj=new XMLHttpRequest();
}
else if(window.ActiveXObject){
obj=new ActiveXObject("Microsoft.XMLHTTP");
}
obj.onreadystatechange=getInfo;
obj.open("GET",url,true);
obj.send();
}

function getInfo(){
if(obj.readyState==4){
var val=obj.responseText;
document.getElementById('uk').innerHTML=val;
}
}

</script>
</head>
<body>
    
<form name="frm">
<input type="text" name="num">
<input type="button" value="PrintTable" 
onClick="printTable()">
</form>

<span id="uk"> </span>

</body>
</html>

PHP FILE

<?php
//argument1.php

$num=trim($_GET["val"]);
echo "Table of ".$num." is "."<br>";  
for($i=1;$i<=10;$i++)
{
  echo $i ."*".$num. " =";
  echo $num * $i ."<br>";
}
?>

This is the PHP file that will be responding the Ajax request. It accepts one parameter and return result to the requested web page.

See Also


QUOTES OF THE DAY

Wonder is the first cause of philosophy.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER