Write a simple PHP program which implements Ajax for addition of two numbers

Add.php

<html>
<head>
   <script language="javascript" src="ajax.js">
   </script>
</head>
<body>
<div id="txt" name="txt"></div>
   <form action="javascript:add(document.getElementById('frm'));" name="frm" id="frm">
  <table>
   <tr>
     <td>Enter first number : </td>
     <td><input type="text" id="num1"></td>
     </tr>
     <tr>
     <td>Enter second number : </td>
     <td><input type="text" id="num2"></td>
     </tr>
     <tr>
       <td colspan="2"><input type="submit" name="button" value="show"></td>
     </tr>
</table>
</form>
</body>
</html>

ajax.js

function add(obj)
{
   var XMLHttpRequestObject=false;
   if(window.XMLHttpRequest)
   {
     XMLHttpRequestObject=new XMLHttpRequest();
    }
   else if(window.ActiveXObject)
    {
      XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
    }
   var str1="num1="+document.getElementById("num1").value;
   var str2="&num2="+document.getElementById("num2").value;
   XMLHttpRequestObject.onreadystatechange=show;
  XMLHttpRequestObject.open('POST','value.php',true);
  XMLHttpRequestObject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  XMLHttpRequestObject.send(str1+str2);
  function show()
  {
    if(XMLHttpRequestObject.readyState==4)
    {
     if(XMLHttpRequestObject.status==200)
     {
       result=XMLHttpRequestObject.responseText;
       document.getElementById('txt').innerHTML=result;
     }
   }
  }
}

value.php

<?php
    $num1=$_POST['num1'];
    $num2=$_POST['num2'];
    $num3=$num1+$num2;
    echo $num3;
?>
                     

Comments

Popular posts from this blog

Create an XML file which gives details of books available in “ABC Bookstore” from following categories. 1)      Technical 2)      Cooking 3)      Yoga And elements in each category are in the following format.     …….     …….     ……. Save the file as “Book.xml” Create an application that reads “Book.xml” file into simple XML object. Display attributes and elements. (Hint:- Use simple_xml_load_file() function). 

Consider the following relational database: Project (P_Group_No, Project_Title) Student (Seat no, Name, Class, P_Group_No) Write a PHP script to accept project title and display list of students those who are working in a particular project.