Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 74,740 times

Contents

Related Categories

An Introduction to PHP - Form Data

gez

Form Data

When an HTML document has a form, the action attribute specifies the name of the program to handle the form data. When a PHP document is specified in the action attribute, the PHP document has access to the named fields in the form. Each field is accessible from the PHP arrays $_POST or $_GET. The variable chosen will depend on whether the form's method attribute was get or post.

The following example uses a form to get the user's name and age. The information is then displayed with a PHP document.

simpleForm.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>User Details</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>User Details</h1>
<form id="userDetails" name="userDetails" method="post" action="welcome.php">
<p>
    Enter your Name: <br>
    <input type="text" size="40" value="" name="name"><br>
    Enter your Age: <br>
    <input type="text" size="5" value="" name="age"><br>
    <input type="submit" value="Enter" name="welcome">
</p>
</form>
</body>
</html>

The following is the PHP script that is called from, simpleForm.html.

welcome.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Welcome</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
<?php
    print "Welcome " . $_POST["name"] . "<br>";
    print "I see you are " . $_POST["age"] . " years old! <br>";
?>
</p>
</body>
</html>

Iterating through a Form collection

You may iterate through the form collection, using either of the server variables, $_GET, and $_POST. The following example uses $_POST variable to iterate through the form shown above.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Display Form Collection</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
<?php
    print "Posted variables: <br>";
    reset ($_POST);
    while(list($key,$val) = each ($_POST))
        print $key . " = " . $value . "<br>";
?>
</p>
</body>
</html>

I'm available for contract work. Please visit Juicify for details.

Comments

  • Re: [3703] An Introduction to PHP

    Posted by radha_telanakula on 02 Jan 2007

    Here they show how to send valued in one file to another(both are in same folder)
    but i need to send values in one folder to another

  • Re: [3703] An Introduction to PHP

    Posted by pjlewis on 04 Apr 2006

    Yes, I think you're correct, although you could easily have a line like this:

    $num = $_POST['num'];


    to get around that problem. Which I think is what's missing from this tutor...

  • Re: [3703] An Introduction to PHP

    Posted by keepbalance on 04 Apr 2006

    In the PHP file should it be "$_POST["num"]" ?


    instead of just $num?

  • file download

    Posted by narayan on 21 Feb 2004

    hello

    I have in php a header which brings a save as box , but when file is down loaded it contains the 6 blank spaces at the start of of document .
    I have to avoid storing these blank spaces.Plea...

  • String concatenation

    Posted by jeen on 28 Jan 2004

    You can also do string concatenation like
    [code]
    $h = "Hello";
    $w = "World";

    $message = "$h $w";

    [/code]