First Time in Form

Name:

Value:

HTML and PHP code follows

<html>
<?php
   #
   # get name of the submit button the
   # user clicked on
   #
   $submit=$_POST['submit'];
   #
   # get the data the user entered in the name box
   #
   $name=$_POST['name'];
   #
   # get the data the user entered in the value box
   #
   $text=$_POST['text'];
   #
   # if this is the first time this program is run
   # then the submit button value will be null
   # and this will be displayed
   #
   if ($submit == '' ) {
      echo "<h2>First Time in Form</h2>";
   }
   #
   # if the user clicked on the Add button
   # this code will be executed
   #
   if ($submit == 'Add' ) {
      echo "<h2>We are adding $name with a new value of '$text'</h2>";
      #
      # if this were a real program the logic
      # of adding the data would go here
      #
   }
   #
   # if the user clicked on the Delete button
   # this code will be executed
   #
   if ($submit == 'Delete' ) {
      echo "<h2>We are deleting $name</h2>";
      #
      # if this were a real program the logic
      # of deleting the data would go here
      #
   }
   #
   # if the user clicked on the Edit button
   # this code will be executed
   #
   if ($submit == 'Edit' ) {
      echo "<h2>We are editing $name with a new value of '$text'</h2>";
      #
      # if this were a real program the logic
      # of editing or updating the data would go here
      #
   }
?>
<form action="adddelcha.php" method="post"> 
<input type="submit" name=submit value="Add">
<input type="submit" name=submit value="Delete">
<input type="submit" name=submit value="Edit">
<p>
Name: <input type="text" name="name" value="<?php echo $name;?>">
<p>
Value: <input type="text" name="text" value="<?php echo $text;?>">
</form>
<h2>HTML and PHP code follows</h2>
<blockquote>
<pre>
<?php
   #
   # now just for fun read in this file 
   # convert it to HTML and display
   # it on the web page
   #
   # read in the file
   #
   $php_and_html_code=file_get_contents($_SERVER['SCRIPT_FILENAME']);
   #
   # convert & to &amp;
   #
   $php_and_html_code=preg_replace("/&/","&amp;",$php_and_html_code); 
   #
   # convert < to &lt;
   #
   $php_and_html_code=preg_replace("/</","&lt;",$php_and_html_code); 
   #
   # convert > to &gt;
   #
   $php_and_html_code=preg_replace("/>/","&gt;",$php_and_html_code); 
   #
   # display the code
   #
   echo $php_and_html_code;
?>
</pre>
</blockquote>
</html>