action attribute. <form action="action_url">
... <!-- form elements go in here. -->
</form>
We will look at how to process information entered into the following form
<form action="process_form1.php">
<!-- When the submit button is clicked, send -->
<!-- form data to the PHP page process_form1.php -->
<input type="text"
name="username"
value="Type in your name" />
<input type="text"
name="the_word"
value="Enter word of the day" />
<input type="submit" /><input type="reset" />
</form>
username has value "Type in your name"the_word has value "Enter word of the day"username=Type+in+your+name&the_word=Enter+word+of+the+day
http://process_form1.php?username=Type+in+your+name&the_word=Enter+word+of+the+day
$_GET["form_variable_name"]
_GET[]. $_GET["username"]
$_GET["the_word"]_ is part of the name.
<html>
<head><title>First PHP form processing</title></head>
<body>
<h1>Processing a Form with PHP</h1>
<h2>The values entered into the form:</h2>
<p>Hi there, <?php echo $_GET["username"]; ?>.<br />
Your word is <?php echo $_GET["the_word"]; ?>.
</p>
<h2>Some environment variables:</h2>
<p>This document is located at
http://<?php echo $_SERVER["HTTP_HOST"], $_SERVER["PHP_SELF"]; ?>.<br />
You requested it from the page <?php echo $_SERVER["HTTP_REFERER"]; ?>.
</p>
<p>The HTTP request method used is
<?php echo $_SERVER["REQUEST_METHOD"]; ?>.<br />
The complete query string is
<?php echo $_SERVER["QUERY_STRING"]; ?>.
</p>
</body>
</html>
The directory for example 1 comtains
In the example1, the form was in one document and the processing PHP in another. Often it is convenient to have the form and the PHP in the same document.
<h1>Process a form with PHP</h1>
<h2>The form</h2>
<form action="form2.php">
<!-- When the submit button is clicked, send -->
<!-- form data to the PHP page process_form.php -->
<p><input type="text"
name="username"
value="Type in your name" /></p>
<p><input type="text"
name="the_word"
value="Enter word of the day" /></p>
<p><input type="submit" /><input type="reset" /></p>
</form>
<h2>The results</h2>
<p>Value of username: <?php echo $_GET["username"]; ?> </p>
<p>Value of the_word: <?php echo $_GET["the_word"]; ?> </p>
The directory for example 2 comtains
Before the form in example 2 is submitted, there aren't any results to show.
This version only shows the results if there are values.
<h1>Process a form with PHP - if there are data</h1>
<h2>The form</h2>
<form action="form3.php">
<!-- When the submit button is clicked, send -->
<!-- form data to the PHP page process_form.php -->
<p><input type="text"
name="username"
value="Type in your name" /></p>
<p><input type="text"
name="the_word"
value="Enter word of the day" /></p>
<p><input type="submit" /><input type="reset" /></p>
</form>
<p><?php
if ($_GET["username"] != "" || $_GET["the_word"] != "")
{
echo "<h2>The results</h2>";
echo "<p>Value of username:" , $_GET["username"], "</p>";
echo "<p>Value of the_word:" , $_GET["the_word"], "</p>";
}?>
The directory for example 3 comtains
method attribute in the form tag.
<body>
<?php
if ($_POST["username"] != "" || $_POST["comments"] != "") {
//If some form data are submitted, tell it back
//the echoed items are separated by commas.
//The variables are not inside quotes
echo "<p>Hi there, ",
$_POST['username'],
". </p><p>Your comments are: <br /> ",
$_POST['comments']. "</p>
";
}
else {
//If nothing submitted, show them the form
//The PHP string is enclosed in single quotes
// because the HTML requires double.
echo '
<form action="form4.php" method="post">
<p>Your name: <input type="text"
name="username"
value="" />
<br />
Comments:<br /><textarea name="comments"
rows="5" cols="60"></textarea><br />
<input type="submit"/> <input type="reset"/></p>
</form>
';
}
?>
<hr />
<p>This document is located at:
http://<?php echo $_SERVER["HTTP_HOST"], $_SERVER["PHP_SELF"]; ?>.<br />
The complete query string was: <?php echo $_SERVER["QUERY_STRING"]; ?>
</p>
</body>
The directory for example 4 comtains
Be sure to add an entry for the lab to your table of contents page and to complete the submission on Blackboard.
Hood College Department of Computer Science: Course materials © 1997-2006 by Elizabeth Chang.