Tuesday, December 8, 2009

Two methods to run PHP script

This article explains different methods to write PHP scripts. Basically PHP is a HTML, without using PHP script in it. A PHP script can be inserted between <?php and ?> tags. General contents can be simply displayed using HTML web page to reduce PHP server script runtime duration.

HTML and PHP

HTML: add.html
<html>
<head>
  <title>Addition of Two Numbers</title>
</head>
<body>
  <form action="add.php" method="post">
    Enter the first number: <input type="text" name="a"><br/>
    Enter the second number: <input type="text" name="b"><br/>
  <input type="submit" name="submit" value="ADD">
  </form>
</body>
</html>
PHP: add.php
<html>
<head>
  <title>Addition of Two Numbers</title>
</head>
<body>
  <?php
  $a = $_POST["a"];
  $b = $_POST["b"];
  $c = $a + $b;
  print "Sum of $a and $b is $c.";
  ?>
</body>
</html>

Only using PHP

HTML: index.php
<?php
$a = $_POST["a"];
$b = $_POST["b"];
if (!isset($_POST["add"])) {
?>
<html>
<head>
  <title>Addition of Two Numbers</title>
</head>
<body>
  <form method="post" action="<?php echo $PHP_SELF; ?>">
    Enter the first number: <input type="text" name="a"></br/>
    Enter the second number: <input type="text" name="b"></br/>
    <input type="submit" value="ADD" name="add">
  </form>
  <?php
  } else {
    $c = $a + $b;
    print "Sum of $a and $b is $c.";
  }
  ?>
</body>
</html>

0 comments :

Post a Comment