PHP
PHP introduction
PHP basics
PHP variables
PHP functions
PHP conditions
PHP loops
PHP arrays
PHP OOP
PHP strings
PHP forms
PHP entitites
PHP files
PHP include files
PHP date and time
PHP cookies
PHP databases
PHP sessions
PHP summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
VBScript
AJAX

Server scripting

ASP

Making money online

Make money online

PHP forms

With PHP, you can get data from forms and work with that data to achieve various things.

This lesson focuses on:

Getting data from a user submitted form

Getting data from a user submitted form requires at least one page - that is, the page from which the form was submitted.

Take a look at this code for a form:

<form name="formOne">
Name: 
<input type="text" name="name" size="15" maxlength="20" />

Pick a random number from 1 to 10: 
<input type="text" name="aNumber" size="8" maxlength="2" />
</form>

By its code, the above form cannot be submitted. The following code contains a modified version of the above form which makes it possible to submit it:

<form name="formOne" method="POST" action="page2.php">
Name: 
<input type="text" name="name" size="15" maxlength="10" />

Pick a random number from 1 to 10: 
<input type="text" name="aNumber" size="8" maxlength="2" />

<input type="submit" />
</form>

Notice the difference between this code and the code for the first form. Specifically, it is in the <form> tag and the addition of a submit button. The attributes added to the <form> tag are method and action. The method attribute states through which method the form will be submitted and the action attribute states to which page the form will be submitted. The action attribute is set to page2.php. It is the code of this page (page2.php) that needs to be modified to get the data from the form that is submitted to it.

We now know that when this form is submitted it will be submitted using the POST method, the data will be sent to a page called page2.php and there will be two variables sent to page2.php - those variables being the values from the fields in the form. Whenever a form is submitted, the number of fields in it will be the number of variables sent to the page to which it is submitted.

Declaring variables for the data from a user submitted form

So now that the data is submitted, how do we declare variables for it? How do we get that data? We must know the name of the submitted forms fields to do so. In the source code above there were two fields - one named 'name' and the other named 'aNumber'.

Syntax for declaring variables for data submitted from a form:

$varName = $_REQUEST["nameOfFormField"];

The $_REQUEST variable is an associative array which holds all of a submitted forms values. A variable is declared set to equal the value of a certain form field.

The following is an example using the fields from the form from above:

<?php
$theName = $_REQUEST["name"];
$theNumber = $_REQUEST["aNumber"];
?>

In the above example two variables are declared. The first variable gets the value of the form field named "name". The second variable gets the value of the form field named "aNumber".

Displaying the data from a user submitted form on a webpage

Displaying the data on a webpage from a user submitted form involves printing the appropriate variables.

Example:

<?

//get the variables
$varOne = $_REQUEST["$varOne"]; $varTwo = $_REQUEST["$varTwo"]; //print the variables print "The value of the first variable is $varOne"; echo "The value of the second variable is $varTwo"; ?>

In the above example, the variable $varOne gets the value of the form field named $varOne, and the variable $varTwo gets the value of the form field named $varTwo from a submitted form.

Different methods of sending data

The form presented earlier in this lesson was submitted using the POST method. There is another method that can be used for form submission and that is the GET method.

The difference between the POST and the GET method:

The POST method puts the values from a form into the headers of an HTTP communication, while The GET method puts the values from a form right into the URL.

When getting data from a submitted form, the $_REQUEST variable is not the only choice. When a form is submitted using the GET method, you can use the $_GET variable, and when a form is submitted using the POST method, you can use the $_POST variable. The $_REQUEST variable can be used for any form, whether it is submitted using the GET method or the POST method.

Sending data to a page without using a form

It is possible to send data to a page without using a form. This is accomplished by supplying the appropriate data in the URL itself.

Example:

Searching for the term "apple" with the search engine at www.google.com yields a page with the URL http://www.google.com/search?hl=en&q=apple&btnG=Google+Search

The search string 'apple' was sent to the google search page. In this case, sending data to a page without using a form would mean manually altering the URL by putting the search term in it without actually using the search form.

A URL such as http://www.google.com/search?hl=en&q=orange&btnG=Google+Search can be used for this task.

Entering this URL into a web browser would run the search through the google search engine and return the result, without having to use the search form.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments