You are on page 1of 2

Program :

index.html
<html>
<head>
<title>AJAX Call to check a Number is prime or not</title>

<script type="text/javascript">
function call() {
var num = parseInt(form.num.value);
var req = new XMLHttpRequest();
req.open("GET", "http://localhost/PhPfolder/prime.php?num=" + num,
"true");
req.send();
req.onreadystatechange = function() {
console.log("Reached call");
if (req.readyState == 4 && req.status == 200) {
document.getElementById("result").innerHTML =
req.responseText + "<hr>";
}
}
}
</script>
</head>
<body>
<h1>AJAX CALLING</h1>
<form name="form">
<input type="text" id="num" name="num" />
<input type="button" value="callPHP-Program" onclick="call()" />
</form>
<h2>PhP server response is shown below!</h2>
<div id="result"> </div>
<hr>
</body>
<style>
div {
background: rgb(255, 104, 104);
color: rgb(0, 0, 0);
margin: 12px;
padding: 10px;
}
</style>

</html>
prime.php
<?php
header("Access-Control-Allow-Origin: *");
$n = $_GET['num'];
echo "<h3>The Entered Number is : ".$n."</h3>";
if(PrimeCheck($n))
{
echo "<h3>The Number is Prime!</h3>";
}else{
echo "<h3>The number is not Prime!</h3>";
}
function PrimeCheck($n)
{
for($i=2; $i<=$n/2; $i++)
if($n%$i==0)return false;
return true;
}
?>

Output :

You might also like