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

YIHENG CHIU
YIHENG CHIU
2,761 Points

I don't know how to apply the concept of $name=$_POST["name"] into HTML.

Can anyone teach me how to solve this challenge?

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,075 Points

First and foremost we need to add a few attributes to the form element. Because we are posting the results of the form to the process.php file, we add a method attribute with the value post to form element. We also have to specify which file will be handling the post which in this case is process.php, so we add another attribute called action with the value of process.php.

<form method = "post" 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>

Now we need to allow the process.php to find the value that is selected in the flavor dropdown list. You would think that we would just use the i.d attribute to get the value of that element, like in JavaScript, however, PHP doesn't read the id of a form element, so we need to give that form element a name attribute. The name attribute functions like id for an element, but it can only be used (don't quote me on that) with php (mainly for form processing). We are going to give this element the name 'flavor' that way it works for the challenge (know that outside of the challenge this name value doesn't need to be the same as the id/class).

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

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

Finally the challenge asks us to create an element that will hold the order_id when the form is submitted, but it must not display the value to the browser. We know that we have to use an input element, and you should have learned about one such type called hidden, so we will create and add that too the form as well, we will set its name to be order_id so that the php script can find it and access its value, we will give then give it the value specified (i forgot the number so do not copy this number, but the one supplied to you). And WE ARE DONE!!!! Hopefully this was of assistance to you.

<form method = "post" action = "process.php">
        <input type="hidden" name="order_id" value="1234">
        <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>
YIHENG CHIU
YIHENG CHIU
2,761 Points

Thank you very much for the clear explanation!!!!

Thank you very much this has helped me with the challenge!

Thank you very much! Great Explanation