HTML forms, PHP form processing

The HTML file with the form:


<html>
<body>

<form name="theform" method="POST" 
action="http://rynite.morris.umn.edu/~elenam/php_examples/forms/form.php">
<table border="0">
<tr>
<td>
Your name goes here:
</td>
<td>
<input type="text" name="name" value="Joe" width="30">
</td>
</tr>
<tr>
<td>
What is your favorite food?
</td>
<td>
<input type="text" name="food" value="apples" width="30">
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>

</body>
</html>
http://rynite.morris.umn.edu/~elenam/php_examples/forms/forms.html

The php file that handles the form data


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- 
Processing form data in PHP
Author: Elena Machkasova elenam@morris.umn.edu 
Last modified: 3/28/06 
--> 
<?php
$name = $_POST["name"];
$food = $_POST["food"];
?>
<html>
<head>
<title>
Form data processing	
</title>
</head>
<body>
<?php
print "$name likes $food!!!<br/>\n";
?>
</body>
</html>

Checking if a form has been submitted; redirection


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Links between two pages
Author: Elena Machkasova
Last Modifed: 3/30/2006
-->
<?php
$post = $_POST["post"];
?>
<html>
<head>
<title>
Linking two pages
</title>
</head>
<body>
<?php
$form_string="
<form name=\"theform\" method=\"POST\" action = \"form_page.php\"
<table border=\"0\">
<tr>
<td>Select the post number:</td>
<td>
<select name=\"post\">
<option value=\"5\">5
<option value=\"7\">7
<option value=\"8\">8
<option value=\"10\">10
<option value=\"11\">11
</select>
</tr>
<tr>
<td>
<input type=\"submit\" value=\"submit\">
</td>
</tr>
</table>
</form>
";

if (!isset($post)) {
  print $form_string;
 }
 else {
   header("Location: links_processing.php?post=$post");
   // print "<meta http-equiv='refresh' content='0; url=links_processing.php?$post'";
 exit();
 }
?>
</body>
</html>


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
 // get the page data (should before html header)
$post = $_GET["post"];

function showerror()
{
  die("Error ". mysql_errno(). " : " .mysql_error());
}
?>
<!--
Links between two pages
Author: Elena Machkasova
Last Modifed: 3/30/2006
-->
<html>
<head>
<title>
Linking two pages
</title>
</head>
<body>
<pre>
<?php
              // connect to the server
if (! ($connection = @mysql_connect("localhost","1101readonly","readonly")))
  die ("connection to the database failed");

// select a database
if (!@mysql_select_db("1101fall06", $connection)) showerror();

// define the query
$q1 = "SELECT * FROM wp_posts WHERE ID = $post;";

if (! ($result = @mysql_query($q1, $connection))) {
  showerror();
 }

while($row = @mysql_fetch_array($result,MYSQL_NUM))
  {

    foreach($row as $attribute)
      {
        print "$attribute\n";
      }
    print "<hr/>\n"; // added for readability
  }

// clean-up:

// free the memory used by the result - helps efficiency
@mysql_free_result($result);

// need this, unless using mysql_pconnect
@mysql_close($connection);
?>
</pre>
</body>
</html>

UMM CSci 1101

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.