10_phpformhandling

Upload: sagar-kadam

Post on 07-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 10_PHPFormHandling

    1/13

    PHP FormHandling

  • 8/4/2019 10_PHPFormHandling

    2/13

    The most important thing to notice when dealingwith HTML forms and PHP is that any form elementin an HTML page will automatically be availableto your PHP scripts.

    The PHP $_GET and $_POST variables are used toretrieve information from forms, like user input.

    The example HTML page contains two input fieldsand a submit button. When the user fills in this

    form and click on the submit button, the form datais sent to the "welcome.php" file.

  • 8/4/2019 10_PHPFormHandling

    3/13

    Name:

    Age:

  • 8/4/2019 10_PHPFormHandling

    4/13

    The "welcome.php" file looks like this:

    Welcome .


    You are years old.

  • 8/4/2019 10_PHPFormHandling

    5/13

    PHP $_GET variable

    The $_GET variable is used to collectvalues from a form with method="get".

    The $_GET variable is an array of variable

    names and values sent by the HTTP GETmethod.

    Information sent from a form with the GET

    method is visible to everyone (it will bedisplayed in the browser's address bar)and it has limits on the amount ofinformation to send (max. 100 characters).

  • 8/4/2019 10_PHPFormHandling

    6/13

    So this method should not be used when sendingpasswords or other sensitive information!

    However, because the variables are displayed inthe URL, it is possible to bookmark the page. This

    can be useful in some cases. The HTTP GET method is not suitable on large

    variable values; the value cannot exceed 100characters.

  • 8/4/2019 10_PHPFormHandling

    7/13

    Example

    Name:

    Age:

    When the user clicks the "Submit" button, the URLsent could look something like this:

    http://www.w3schools.com/welcome.php?name=Peter&age=37

  • 8/4/2019 10_PHPFormHandling

    8/13

    The "welcome.php" file can now use the $_GETvariable to catch the form data (notice that thenames of the form fields will automatically be theID keys in the $_GET array):

    Welcome .

    You are years old!

  • 8/4/2019 10_PHPFormHandling

    9/13

    PHP $_POST Variable

    The $_POST variable is used to collect values froma form with method="post".

    The $_POST variable is an array of variable namesand values sent by the HTTP POST method.

    Information sent from a form with the POSTmethod is invisible to others and has no limits onthe amount of information to send.

    However, because the variables are not displayedin the URL, it is not possible to bookmark the page.

  • 8/4/2019 10_PHPFormHandling

    10/13

    Example

    Enter your name: Enter your age:

    When the user clicks the "Submit" button, the URLwill not contain any form data, and will look

    something like this: http://www.w3schools.com/welcome.php

  • 8/4/2019 10_PHPFormHandling

    11/13

    The "welcome.php" file can now use the $_POSTvariable to catch the form data (notice that thenames of the form fields will automatically be theID keys in the $_POST array):

    Welcome .

    You are years old!

  • 8/4/2019 10_PHPFormHandling

    12/13

    The $_REQUEST Variable

    The PHP $_REQUEST variable contains thecontents of both $_GET, $_POST, and $_COOKIE.

    The PHP $_REQUEST variable can be used to getthe result from form data sent with both the GETand POST methods.

    Example

    Welcome .
    You are

  • 8/4/2019 10_PHPFormHandling

    13/13

    Form Validation

    User input should be validated whenever possible.Client side validation is faster, and will reduceserver load.

    However, any site that gets enough traffic to worryabout server resources, may also need to worryabout site security. You should always use serverside validation if the form accesses a database.

    A good way to validate a form on the server is topost the form to itself, instead of jumping to a

    different page. The user will then get the errormessages on the same page as the form. Thismakes it easier to discover the error