News:

Build a stunning handcrafted website with IT Acumens

Main Menu

Learn PHP – Forms !!

Started by krishnan, Jul 21, 2008, 08:26 PM

Previous topic - Next topic

krishnan

Hi [you],

In the PHP Tutorial you will learn about PHP Forms - Predefined variables, Reading input from forms and Using hidden fields to save state.

Predefined variables:

PHP has several predefined variables called superglobals.
Superglobals are always present and available in any PHP script.
The superglobals are arrays of other variables.
PHP superglobals that work with forms:

• $_GET contains any variables provided to a script through the GET method.
• $_POST contains any variables provided to a script through the POST method.
• $_FILES contains any variables provided to a script through file uploads.


Reading input from forms:

Lets use lesson 1 example:

    <html>
    <title>Processing Forms</title>
    <body>
    <form method="POST" action="process.php">
    Name: <input type="text" name="name">
    <p>
    Sex: <input type="radio" name="sex" value="M"> Male
    <input type="radio" name="sex" value="F"> Female
    <p>
    Annual Income:
    <select name="income">
    <option value="$25,000">Under $25,000
    <option value="$25,000 to $50,000">$25,000 to $50,000
    <option value="$50,000 and higher">$50,000 and higher
    </select>
    <p>
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>


This form is sent using POST request, we need now to create a PHP script that is going to read the sent data, let's suppose that user entered "John Smith" for name, "Male" for sex, and selected "$25,000 to $50,000" for annual income.

    <html>
    <head>
    <title>process.php</tite>
    </head>
    <body>
    <?php
    print "Name: $_POST['name']
";
    print "Sex: $_POST['sex']
";
    print "Annual income: $_POST['income']
";
    ?>
    </body>
    </html>


The variables name, sex, and income are accessed using the $POST superglobal.
$POST is an array that contains all the data sent using POST request, if GET is used as the form method instead of GET, then we could access it using $GET superglobal.

Using hidden fields to save state:

Sometimes the script uses the form data to create another form, this form will be submitted to another PHP script, some of the first form data is needed in the final script.

To do so, you can use hidden fields.


Example:

    <html>
    <title>Page1</title>
    <body>
    <form method="POST" action="process1.php">
    Name: <input type="text" name="name">
    <p>
    Sex: <input type="radio" name="sex" value="M"> Male
    <input type="radio" name="sex" value="F"> Female
    <input type=" submit" name=" submit" value=" Submit">
    </form>
    </body>
    </html>


This html document has two form fields, the text field "name", and the radio button "sex", the data is entered by user, and then submitted to the PHP script "process1.php", here is the code for "process1.php":

    <html>
    <title>Page2</title>
    <body>
    <form method="POST" action="process2.php">
    <input type="hidden" name="form_name" value="<?php print $_POST['name'] ?>">
    Gift:
    <p>
    <?php
    if($_POST['sex'] == 'W') {
    print "<input type='radio' name='gift' value='Bracelet'>Bracelet
\n
    print "<input type='radio' name='gift' value='Earrings'>Earrings
\n
    } else {
    print "<input type='radio' name='gift' value='Watch'>Watch
\n
    print "<input type='radio' name='gift' value='Tie'>Tie
\n
    }
    ?>
    <input type=" submit" name=" submit" value=" Submit">
    </form>
    </body>
    </html>


"process1.php" will create another document that has two form elements, the hidden field "form_name", the value of it is the value of the "name" text field of the html document, the other form element is the radio button of the gifts to choose from, the code of these radio buttons depend on the value of the "sex" text field of the html file, after the PHP file is processed, sent to the user, filled by user and submitted, here is the code for "process2.php":

    <html>
    <title>Process2.php</title>
    <body>
    Name: <?php print $_POST['form_name'] ?>

    Gift: <?php print $_POST['gift'] ?>
    </body>
    </html>
;


"procees2.php" printed user selections, note that the user name that's displayed here is the name sent from the hidden field "form_name" of "process1.php".