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). 

Write a PHP program to create a simple calculator that can accept two numbers and perform operations like add, subtract, multiplication and divide (using Self Processing form)

Write a script to solve following questions(use “Book.xml” file) A:- Create a Dom document object and load this xml file. B:- Get the output of this document to the browser. C:- Save this [.xml] document in another format that i.e. in [.doc]. D:- Write a xml program to print the names of the books available in “Book.xml” file.