Tuesday, December 8, 2009

Two methods to run PHP script

HTML with PHP :-

HTML: add.htm
<html>
<head>
<title>Adding two numbers</title>
</head>
<body>
<form action=
add.php” method=post”>
Enter the first number: <input type=
text” name=a” size=10><br>
Enter the second number: <input type=
text” name=b” size=10><br>
<input type=
submit” name=submit” value=ADD”>
</form>
</body>
</html>


PHP: add.php
<html>
<head>
<title>Adding two numbers</title>
</head>
<body>
<?php
$a=$_POST[
a];
$b=$_POST[
b];
$c=$a+$b;
print “Addition of $a and $b is $c.”;
?>
</body>
</html>


Only with PHP :-
<?php
$a = $_POST[
a];
$b = $_POST[
b];
if (!isset($_POST[
add]))
{ // If page is not submitted to itself echo the form
?>
<html>
<head>
<title>Adding Two Numbers</title>
</head>
<body>
<form method=
post” action=<?php echo $PHP_SELF; ?>”>
Enter the first number: <input type=
text” size=12 maxlength=12 name=a”></br>
Enter the second number: <input type=
text” size=12 maxlength=36 name=b”></br>
<input type=
submit” value=ADD” name=add”>
</form>
<?php
}
else
{
$c=$a+$b;
print “Addition of $a and $b is $c.”;
}
?>
</body>
</html>

0 comments :

Post a Comment