Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,165 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,165 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,165 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>