Web Technologies

Web Technologies 

Slip21

Q.1) Design a HTML form to accept two numbers from the user. Give options to choose the arithmetic operation (use radio buttons). Write a PHP function to display the result on the next form. (Use the concept of function and default parameters)   [Marks30] 

Solution

a.php
<?php
function add($a=0,$b=0)
{
return $a+$b;
}
function sub($a=0,$b=0)
{
return $a-$b;
}
function mult($a=0,$b=0)
{
return $a*$b;
}
function div($a=0,$b=1)
{
return $a/$b;
}
function mod($a=0,$b=1)
{
return $a%$b;
}
$x=$_POST['n1'];
$y=$_POST['n2'];
$op=$_POST['op'];
switch($op)
{
case 1:if(isset($x) && isset($y))
printf("Answer=%d",add($x,$y));
       else
printf("Answer=%d",add());
       break;
case 2:if(isset($x) && isset($y))
printf("Answer=%d",sub($x,$y));
       else
printf("Answer=%d",sub());
       break;
case 3:if(isset($x) && isset($y))
printf("Answer=%d",mult($x,$y));
       else
printf("Answer=%d",mult());
       break;
case 4:if(isset($x) && isset($y))
printf("Answer=%f",div($x,$y));
       else
printf("Answer=%f",div());
       break;
case 5:if(isset($x) && isset($y))
printf("Answer=%d",mod($x,$y));
       else
printf("Answer=%d",mod());
       break;
}
?>


b.html
<form method='post' action='a.php'>
Number1 :<input type='text' name='n1'><br>
Number2 :<input type='text' name='n2'><br>
<b>Select option :</b><br>
<input type='radio' name='op' value=1>add(+)
<input type='radio' name='op' value=2>sub(-)
<input type='radio' name='op' value=3>mult(*)
<input type='radio' name='op' value=4>div(/)
<input type='radio' name='op' value=5>mod(%)
<br>
<input type='submit'>
<input type='reset'>
</form>


BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post