Examples of PHP form display and handling

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
Using form data to create a link
Author: Elena Machkasova
Last Modifed: 4/4/2007
$post_num = $_POST["post_num"];
$submitted = $_POST["submit"];

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

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))) {

	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

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.