Calculating Square Root using jQuery Ajax & PHP

Calculating Square Root using jQuery Ajax & PHP


Posted in : PHP Posted on : January 13, 2011 at 7:00 PM Comments : [ 0 ]

This section contains the detail about Calculating Square Root using jQuery Ajax & PHP

Calculating Square Root using jQuery Ajax & PHP

In this section, you will learn how to calculate Square root of the given number using jQuery & PHP.

JQuery Ajax is used to submit data from user input form to PHP script and also for displaying result on the same page.

Example :

FindSquareRoot.php

<html>
<head>
<title>Simple jQuery and PHP Square Root example</title>
<script src="js/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript"> 
$(document).ready(function() {
$('#form').submit(function(){
var number = $('#number').val(); 
$.ajax({type:"post",url:"calculate.php",data:"number=" 
+number,success:function(msg){$('#result').hide(); 
$("#result").html("<h3>" + msg + "</h3>").fadeIn("slow"); } });
return false;
});
});
</script>
</head>
<body>
<form id="form" action="calculate.php" method="post">
Enter number:
<input id="number" type="text" name="number" />
<input id="submit" type="submit" value="Calculate Square Root" 
name="submit"/>
</form>
<p id="result"></p>
</body>
</html>

calculate.php

<?php
if($_POST['number']==null){
echo "Please Enter a Number";
}else {
if (!is_numeric($_POST['number'])) {
echo "Please enter only numbers";
}else{
echo "Square Root of " .$_POST['number'] ." is ".sqrt($_POST['number']);
}
}
?>

Output :

It will show you square root of the provided number :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics