An HTML form is a collection of HTML elements embedded within a page. By adding different types of elements, you can create different form fields, such as text fields, pull-down menus, checkboxes, and so on.
All Web forms start with an opening <form> tag, and end with a closing </form> tag:
<form action="myscript.php" method="POST"> <!-- Contents of the form go here --> </form>
There are two attributes within the opening <form> tag:
<form action="someform.php" method="post"> Name: <input type="text" name="Name" value="Jim" /><br /> Password: <input type="password" name="Password" /><br /> Age: <input type="text" name="Age" /><br /> <input type="submit" /> </form>
To read the data from a form, we can use the following three superglobal variables.
Superglobal Array | Description |
---|---|
$_GET | Contains all the field names and values sent by a form using the get method |
$_POST | Contains all the field names and values sent by a form using the post method |
$_REQUEST | Contains the values of both the $_GET and $_POST arrays combined, along with the values of the $_COOKIE superglobal array |
Each of these three superglobal arrays contains the field names from the sent form as array keys, with the field values themselves as array values.
<input type="text " name="emailAddress" value="" />
You could then access the value that the user entered into that form field using either the $_GET or the $_REQUEST superglobal:
$email = $_GET["emailAddress"]; $email = $_REQUEST["emailAddress"];
GET
sends its variables in the URL.
It easy to see what was sent with GET
.
And user can change what was sent
There is usually a low limit on the number of characters that can be sent in a URL.
If we try to send long variables using GET, we may lose some of them.
POST
sends its variables behind the scenes and has a
much higher limit.
It limit is usually several megabytes.
Browsers will not automatically resend
post data if your user clicks the Back button,
you may get a message like
"The data on this page needs to be resent".
This does not happen with GET
, browsers will just resend data as needed through URL.
PHP has post_max_size
entry in php.ini.
It is usually set to 8M by default, which is 8 megabytes.
Name the following file as index.htm file.
<!DOCTYPE html5> <html> <body> <form action="index.php" method="post"> <label for="firstName">First name</label> <input type="text" name="firstName" id="firstName" value="" /> <label for="lastName">Last name</label> <input type="text" name="lastName" id="lastName" value="" /> <label for="password1">Choose a password</label> <input type="password" name="password1" id="password1" value="" /> <label for="password2">Retype password</label> <input type="password" name="password2" id="password2" value="" /> <label for="genderMale">Are you male...</label> <input type="radio" name="gender" id="genderMale" value="M" /> <label for="genderFemale">...or female?</label> <input type="radio" name="gender" id="genderFemale" value="F" /> <label for="favoriteWidget">What's your favorite widget?</label> <select name="favoriteWidget" id="favoriteWidget" size="1"> <option value="superWidget">The SuperWidget</option> <option value="megaWidget">The MegaWidget</option> <option value="wonderWidget">The WonderWidget</option> </select> <label for="newsletter">Do you want to receive our newsletter?</label> <input type="checkbox" name="newsletter" id="newsletter" value="yes" /> <label for="comments">Any comments?</label> <textarea name="comments" id="comments" rows="4" cols="50"> </textarea> <div style="clear: both;"> <input type="submit" name="submitButton" id="submitButton" value="Send Details" /> <input type="reset" name="resetButton" id="resetButton" value="Reset Form" style="margin-right: 20px;" /> </div> </form> </body> </html>
Next, save the following script as index.php in the same folder with index.html.
<!DOCTYPE html5> <html> <body> <p>Thank you for registering. Here is the information you submitted:</p> <dl> <dt>First name</dt><dd><?php echo $_POST["firstName"]?></dd> <dt>Last name</dt><dd><?php echo $_POST["lastName"]?></dd> <dt>Password</dt><dd><?php echo $_POST["password1"]?></dd> <dt>Retyped password</dt><dd><?php echo $_POST["password2"]?></dd> <dt>Gender</dt><dd><?php echo $_POST["gender"]?></dd> <dt>Favorite widget</dt><dd><?php echo $_POST["favoriteWidget"]?></dd> <dt>Do you want to receive our newsletter?</dt><dd><?php echo $_POST["newsletter"]?></dd> <dt>Comments</dt><dd><?php echo $_POST["comments"]?></dd> </dl> </body> </html>