Wednesday, 27 December 2017

AWT-Slip12

1). Write a PHP script for the following: Design a form to accept a number from the user. Perform the operations and  show the results. 1) Factorial of a number using Recursion.2)      Add the digits of that number to reduce it to single digit.(use the concept of self processing page.)  

fac12.html
<html>
<body>
<form action="facc12.php" method="Get">
<center>
<br><h1>Please enter number and select option</h1>
<br>Enter number : <input type="number" name="num"><br><br>
<table>

<tr><td><input type="radio" name="op" value="1">1.Find Factorial</td></tr>
 <tr><td><input type="radio" name="op" value="2">2.Sum of Digits</td></tr>
 <tr><td><input type="radio" name="op" value="3">3.Show Even numbers till given number</td>
   </tr>
</table>
<br><input type="Submit" name="submit" value="Submit"><input type="Reset" name="reset" value="Reset">
 </center></form></body></html>

 facc12.php

<?php
echo "<center><h3><br><br>";
function Factorial($num)
{
if($num==0 || $num==1)
return 1;
return $num*Factorial($num-1);
}
function SumOfDigits($num)
{
$sum = 0;
while($num > 0)
{
while($num != 0)
{
$sum += $num%10;
$num=$num/10;
}
if($sum > 9)
{
$num = $sum;
$sum = 0;
}          
}
return $sum;
}
function PrintEvenNo($num)
{
echo "Printing Even Number till $num : <br>";
for($i=2;$i<$num;$i++)
{
if($i%2==0)
echo "$i<br>";
}
}
if(isset($_GET['op']))
{
$op = $_GET['op'];
if(isset($_GET['num']))
{
$num = $_GET['num'];
switch($op)
{
case 1:
$res = Factorial($num);
echo"Factorial of $num is $res";
break;
case 2:
$res = SumOfDigits($num);
echo"Sum of digits of $num is $res";            
break;
case 3:
PrintEvenNo($num);
break;
}
}
else
echo"<h1>Please Enter Number</h1>";
}
else
echo "Please Select Choice";
echo "</h3></center>";
?>


2)Create student table as follows:

Student(sno, sname, standard, Marks, per). Write AJAX script to select the student name and print the student's details of particular standard.

                                                                                                                                     

Students.php

<html>

<head>

<script>

function showHint(str)

{

if (str=="")

{

document.getElementById("mydiv").innerHTML = "";

return;

}

if(window.XMLHttpRequest)

{

XHRobj=new XMLHttpRequest();

}

else

{

XHRobj=new ActiveXObject("Microsoft.XMLHTTP");

}

XHRobj.onreadystatechange = function()

{

if (XHRobj.readyState == 4 && XHRobj.status == 200)

{

document.getElementById("mydiv").innerHTML = XHRobj.responseText;

}

}

XHRobj.open("GET", "select.php?q=" + str, true);

XHRobj.send();

}

</script>

</head>

<body>

<p><b>Student Name:</b></p>

<form>

<?php

$database="test"; //database name

$con =mysqli_connect("localhost","root" ,"","test");//for wamp 3rd feild is balnk

if (!$con)

{

die('Could not connect: ' . @mysqli_error());

}

$result=mysqli_query($con,"select * from student")or die(@mysqli_error());

?>

<SELECT name="sname" onchange="showHint(this.value)">

<option value="">Select Name</option>

<?php while($row=mysqli_fetch_array($result))

{

?>

<option value="<?php echo $row['sname']?>"><?php echo $row['sname']?></option>

 <?php }?>

</select>

</form>

<div id="mydiv"><b> Student info......</b></div>

</body>

</html>

 

Select.php

 

<?php

$database="test"; //database name

$q=$_GET["q"];

$con=@mysqli_connect("localhost","root" ,"", "test");//for wamp 3rd feild is balnk

if(!$con)

{

die('Could not connect: ' . @mysql_error());

}

$result=@mysqli_query($con,"select * from student where sname='$q'")or die(@mysqli_error());

echo"<table border='1'>";

echo"<tr><th>sno</th><th>sname</th><th>per</th></tr>";

while($row=@mysqli_fetch_array($result))

{

echo"<tr><td>";

echo $row['sno'];

echo"</td><td>";

echo $row['sname'];

echo"</td><td>";

echo $row['per'];

echo"</td></tr>";

}

echo"</table>";

@mysqli_close($con);

?>