Examples of PHP form display and handling


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Using form data to create a link
Author: Elena Machkasova
Last Modifed: 4/4/2007
-->
<?php
$post_num = $_POST["post_num"];
$submitted = $_POST["submit"];

function showerror()
{
  die("Error ". mysql_errno(). " : " .mysql_error());
}
?>
<html>
<head>
<title>
Using form data to display a post
</title>
</head>
<body>
<?php
$form_string="
<form name=\"theform\" method=\"POST\" action = \"form_page.php\">
<table style=\"border: none\">
<tr>
<td>Select the post number:</td>
<td>
<select name=\"post_num\">
<option value=\"5\">5
<option value=\"7\">7
<option value=\"8\">8
</select>
</tr>
<tr>
<td>
<input type=\"submit\" name = \"submit\" value=\"submit\">
</td>
</tr>
</table>
</form>
";

if (!isset($submitted) || !isset($post_num)) {
  	print $form_string;
}
else {
  	print "<h1>Displaying post $post_num</h1>\n";
        // connect to the server
	if (! ($connection = @mysql_connect("localhost","1101readonly","readonly")))
  		die ("connection to the database failed");

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

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

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

	print "<pre>";
  	while($row = @mysql_fetch_array($result,MYSQL_NUM))
  	{

    		foreach($row as $attribute)
      		{
        		print "$attribute\n";
      		}
    	print "<hr/>\n"; // added for readability
  	}
  	print "</pre>";
	
	// needed in some cases
	@mysql_close($connection);
}
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_spring07/forms/form_page.php

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.