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

Timothy Lucas
Timothy Lucas
8,618 Points

Error with $_POST array feature on code challenge.

I am currently working on a code challenge that is asking me what changes are needed to be able to access data in the "flavor" element of the $_POST array, like this: $_POST["flavor"]. I currently have the following code:

form action="process.php" method="POST" name="flavor"

    <label for="flavor">Flavor</label>
    <select id="flavor" name="flavor">

Yet I am being told I have added the right attribute (method) to the right element (the form), but it has the wrong value. Hint: we need to set the request method so that values are accessible in the $_POST array.

I know the <> are missing around the form element but it was showing up wierd on the forum with them included.

1 Answer

Alex Heil
Alex Heil
53,547 Points

hey Timothy Lucas , I guess you're referring to the second question in the challenge? this would be: Next, we need the flavor selected in the dropdown to be submitted to this process.php file. We need to be able to access it in the β€œflavor” element of the <code>$_POST</code> array, like this: <code>$_POST["flavor"]</code>. What two changes do we need to make to the HTML? (Hint. We need to add two new attributes. The first needs to be added to one existing HTML element, the second needs to be added to a different existing HTML element.)

so from the question we already know we have to add 2 new elements and that we would have to add them to 2 different parts of the code.

looking at your code you're already close, you added the method and also the name parts. however you added both to the same element, the form. for the method=POST that's absolutely fine, however the name part doesn't go into the form container, instead you want to add it to the select list instead - that's the container that holds the values (the options) that you then later can access when the form gets submitted.

in full the code would look like this:

 <form action="process.php" method="post">

        <label for="flavor">Flavor</label>
        <select id="flavor" name="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>

hope that helps and have a nice day ;)

Timothy Lucas
Timothy Lucas
8,618 Points

Alex,

I appreciate the help. I'm still just slightly confused. I get what you're saying about the form element not needing the name=flavor part but I did also add the name=flavor to the select element as well. I actually added the name to the form element after I had already added it to the select element since it wasn't working. From your code it looks exactly like mine other than the name in the form element.

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

    <label for="flavor">Flavor</label>
    <select id="flavor" name="flavor">
Timothy Lucas
Timothy Lucas
8,618 Points

Alex,

I actually just figured it out comparing our code. I had:

method="POST"

instead of:

method="post"

Thanks for the help!