Page 213 - Open Soource Technologies 304.indd
P. 213
Unit 9: Web Techniques
underscores (_), and the string “HTTP_” is prepended. For example, the entry for the User-Agent Notes
header has the key “HTTP_USER_AGENT”. The two most common and useful headers are:
HTTP_USER_AGENT
The string the browser used to identify itself (e.g., “Mozilla/5.0 (Windows 2000; U) Opera
6.0 [en]”)
HTTP_REFERER
The page the browser said it came from to get to the current page (e.g., “http://www.
example.com/last_page.html”)
9.4 Processing Forms
It is easy to process forms with PHP, as the form parameters are available in the $_GET and
$_POST arrays. There are many tricks and techniques for working with forms.
9.4.1 Create the Form
Let’s look at the form we used for the first tutorial and make a few updates to it.
<form action=”php-form-processor.php” method=”post”>
Which is your favorite movie?
<input type=”text” name=”formMovie” maxlength=”50” value=”<?=$varMovie;?>” />
What is your name?
<input type=”text” name=”formName” maxlength=”50” value=”<?=$varName;?>” />
Please choose your gender?
<select name=”formGender”>
<option value=””>Select...</option>
<option value=”M”>Male</option>
<option value=”F”>Female</option>
</select>
<input type=”submit” name=”formSubmit” value=”Submit” />
</form>
We are still using the post method. The action is now “php-form-processor.php”, since this is a
new example, and we have added a new input: a “select” box, also known as a “drop-down”
or “pull-down” box. A select box contains one or more “options”. Each option has a “value”,
just like other inputs, and also a string of text between the option tags. This means when a user
selects “Male”, the “formGender” value when accessed by PHP will be “M”.
Using Simfatic Forms you can create feature-rich web forms without coding.
9.4.2 Getting the Form Data in the PHP Script
Let’s look at some PHP code to process this form.
<?php
if($_POST[‘formSubmit’] == “Submit”)
{
LOVELY PROFESSIONAL UNIVERSITY 207