PHP
  1. PHP intro
  2. PHP basics
  3. PHP variables
  4. PHP functions
  5. PHP conditions
  6. PHP loops
  7. PHP arrays
  8. PHP classes & objects
  9. PHP strings
  10. PHP forms
  11. PHP entities
  12. PHP files
  13. PHP include files
  14. PHP date & time
  15. PHP cookies
  16. PHP databases
  17. PHP sessions
  18. PHP summary

PHP forms

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

This tutorial 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>

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.

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 $theName = $_REQUEST["name"]; $theNumber = $_REQUEST["aNumber"]; //print the variables print "Your name: " . $theName; echo "<br />"Your number: " . $theNumber; ?>

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.

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.

© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information