Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Integrating with PayPal HTML Forms

Which attribute will be added in html to make it access to $_POST array?

This challenge ask me to add 2 attributes to make this html file access to POST array on the server. I do not have any clues, and the system gave me another hint that 1 new attribute will be added in select element.

form.html
<!DOCTYPE html>
<html>
<head>
    <title>Ye Olde Ice Cream Shoppe</title>
</head>
<body>

    <p>Your order has been created. What flavor of ice cream would you like to add to it?</p>

    <form action="process.php">

        <label for="flavor">Flavor</label>
        <select id="flavor">
            <option value="">&#8212; Select &#8212;</option>
            <option value="Vanilla">Vanilla</option>
            <option value="Chocolate">Chocolate</option>
            <option value="Strawberry">Strawberry</option>
            <option value="Cookie Dough">Cookie Dough</option>
        </select>

        <input type="submit" value="Update Order">

    </form>

</body>
</html>

5 Answers

Dale Hudson
Dale Hudson
20,578 Points

within your form tag add method="post"

I added the method attribute from the beginning, but the system still said error. The system gave me another hint that I need to set the request method for access $_POST array

Dale Hudson
Dale Hudson
20,578 Points

To access the $_POST array open a php block above form and try:

<?php if($_SERVER['REQUEST_METHOD'] == "post") { //Do something } ?>

Don't forget to add the method="post" into your form tag.

I do not understand clearly about method="";. When I study, people always set method="POST / GET" <=capital letter, but the answer is "post". What is difference between capital letter and normal?

Dale Hudson
Dale Hudson
20,578 Points

The method attribute specifies the HTTP method to be used when submitting a form (either GET or POST). POST is used when you are posting data to the server (Like we are in the form below). GET is used to retrieve information. A good example of this is when you search for something in a search engine.

I have included a link to w3schools with information on form attributes including the method attribute.

Regarding setting GET/POST to capitals, I don't think it matters. Just personal choice. (Don't quote me on this though)

http://www.w3schools.com/html/html_forms.asp

<!DOCTYPE html> <html> <head> <title>Ye Olde Ice Cream Shoppe</title> </head> <body>

<?php

    //Check the form has been submitted and request method is equal to post
    if ($_SERVER['REQUEST_METHOD'] == "POST")
    { 
         //Request method is equal to POST, print out the post array in human readable form 
         print_r($_POST);
    }
    else 
    {
         //Request method not equal to POST
         echo "Error submitting form";
    }

?>

<p>Your order has been created. What flavor of ice cream would you like to add to it?</p>

//Create form
<form action="process.php" method="POST">

    <label for="flavor">Flavor</label>
    <select id="flavor" name="flavor">
        <option value="select">&#8212; Select &#8212;</option>
        <option value="Vanilla">Vanilla</option>
        <option value="Chocolate">Chocolate</option>
        <option value="Strawberry">Strawberry</option>
        <option value="Cookie Dough">Cookie Dough</option>
    </select>

    <input type="submit" value="Update Order">

</form>

</body> </html>