PHP POST & GET

Discussion in 'Web Design & Programming' started by RHochstenbach, Mar 8, 2011.

  1. RHochstenbach

    RHochstenbach Administrator Staff Member

    Likes Received:
    26
    Trophy Points:
    48
    POST
    A very essential feature of PHP is the ability to collect the input from HTML forms. As you might know, you start with an HTML form with something like this:
    HTML:
    <form method="post" action="form.php">
    'post' means it passes each field to a PHP variable called $_POST as an array. The name of each form element is passed as the unique name of the array. So a text field named 'name' gets passed to PHP as $_POST['name']. 'action' is the part which tells the form what to do with the information. Do you want to collect it in a different file? Then put the location of that file there. In this case you can save the form with a .htm or .html extension because there is no PHP code in there.

    Otherwise you might choose to have the PHP script inside the same file. In that case, you can leave the 'action' element out. But for the sake of web standards, leave it in there, but put nothing between the quotation marks, like this:
    HTML:
    <form method="post" action="">
    When you do it like this, the file needs to be saved with a .php extension, because it's going to contain PHP code.

    I'm now going to show you an example of an HTML form written in just plain HTML:
    HTML:
    <form method="post" action="">
    Name: <input type="text" name="name" />
    Address: <input type="text" name="address" />
    Phone Number: <input type="text" name="phone" />
    <input type="submit" name="add" value="Add Me" />
    </form>
    
    I have three text fields: Name, Address and Phone Number, each having a unique name which is set in 'name='. If you press the Add Me button (the submit button), it sets the name of the button to 1. In this case, the button is called 'add'.

    To make sure PHP only starts to collect the form data, we need to wrap in inside an IF statement. If the form has been submitted, the 'add' name gets a value.

    PHP:
    if(isset($_POST['add'])) {
    // if the submit button is pressed, it sets the variable $_POST['add'] (where 'add' is the unique name // for that form element).
    }
    Now we output the contents of the form:
    PHP:
    echo "Your name is: ".$_POST['name'];
    echo 
    "<br />"//HTML linebreak
    echo "Your address is: ".$_POST['address'];
    echo 
    "<br />";
    echo 
    "Your phone number is is: ".$_POST['phone'];
    Our code now looks like this:
    Code:
    <html>
    <body>
    <form method="post" action="">
    Name: <input type="text" name="[B]name[/B]" />
    Address: <input type="text" name="[B]address[/B]" />
    Phone Number: <input type="text" name="[B]phon[/B]e" />
    <input type="submit" name="[B]add[/B]" value="Add Me" />
    </form>
    <?php
    if(isset($_POST['add'])) {
    echo "Your name is: ".$_POST['name'];
    echo "<br />"; //HTML linebreak
    echo "Your address is: ".$_POST['address'];
    echo "<br />";
    echo "Your phone number is is: ".$_POST['phone'];
    }
    ?>
    </body>
    </html>
    
    There you go, this is how you collect data from a form.


    GET
    GET is very similar to POST. It also creates an array using the variable $_GET. The major difference here is that it collects the data not from a form, but from the URL.

    Let's say we want to pass the names and prices of some items from a food store in the URL. We would build the URL like this:
    Code:
    prices.php?cheese=2&bananas=1&melons=4
    In this URL we have specified the items cheese, bananas and melons and their price. We can now collect this information.
    PHP:
    echo "Cheese price:".$_GET['cheese'];
    echo 
    "Bananas price:".$_GET['bananas'];
    echo 
    "Melons price:".$_GET['melons'];
    Of course, we can also use a FOREACH loop here to show what variables have been inserted into the URL:
    PHP:
    foreach($_GET as $name => $price) {
    echo 
    "Our product " .$name ." costs: $".$price;
    echo 
    "<br />";
    }
    Save the file as prices.php, and then try different variables with numbers.

    Did you know that an HTML form can generate a URL?
    HTML:
    <h1>Set prices</h1>
    <form method="get" action="prices.php"
    Cheese: <input type="text" name="cheese" />
    Bananas: <input type="text" name="bananas" />
    Melons: <input type="text" name="melons" />
    <input type="submit" name="submit" value="Set Prices" />
    
    Save this as an HTML file with the .htm or .html extension. Now fill in some values and press the Set Prices button. It will then load the prices.php script with the new values set in the URL. The PHP script will now show the products with the corresponding prices.
     

Share This Page