Using HTML forms with PHP: Accessing single and multiple form values

Source: Internet
Author: User
Tags html form php and versions

The ability to easily manipulate the information that users submit through HTML forms has been one of the advantages of PHP. In fact, PHP version 4.1 adds several new ways to access the information and effectively removes one of the most commonly used methods in previous versions. This article studied different ways to use the information submitted on an HTML form and used both earlier versions of PHP and newer versions. This article starts with a study of a single value and then builds a page that can generally access any available form values.

Note: This article assumes that you have access to a Web server running PHP version 3.0 or later. You need to have a basic understanding of PHP itself and the creation of HTML forms.

HTML form

As you read this article, you will see how different types of HTML form elements provide information that PHP can access. For this example, I used a simple information form that consists of two text fields, two check boxes, and a selection box that allows multiple items:

Listing 1. HTML form<body>
<form action= "formaction.php" >
<table width= "100%" >
<tr><td>ship name:</td><td><input type= "text" name= "ship"/></td></tr>
<tr><td>trip date:</td><td><input type= "text" name= "Tripdate"/></td></tr >
&LT;TR&GT;&LT;TD colspan= "2" >mission goals:</td></tr>
<tr>
<td><input type= "checkbox" Name= "Exploration" value= "yes"/>
Exploration</td>
<td><input type= "checkbox" name= "Contact" value= "yes"/>
Contact</td>
</tr>
<tr>
&LT;TD valign= "Top" >crew species: </td>
<td>
<select name= "Crew" multiple= "multiple" >
<option value= "Xebrax" >Xebrax</option>
<option value= "Snertal" >Snertal</option>
<option value= "Gosny" >Gosny</option>
</select>
</td>
</tr>
&LT;TR&GT;&LT;TD colspan= "2" align= "center" ><input type= "Submit"/></td></tr>
</table>
</form>
</body>

In the absence of a method specified, the form uses the default method get, which the browser uses to attach the form value to the URL, as follows:

http://www.vanguardreport.com/formaction.php?
ship=Midnight+Runner&tripdate=12-15-2433&exploration=yes&crew=snertal&crew=gosny

Figure 1 shows the form itself.

Figure 1. HTML form

The old fashioned: accessing global variables

The code shown in Listing 2 handles the form value as a global variable:

Listing 2. Form values as global variables<?php
echo "Ship = ".$ship;
echo "<br />";
echo "Tripdate = ".$tripdate;
echo "<br />";
echo "Exploration = ".$exploration;
echo "<br />";
echo "Contact = ".$contact;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.