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

Ian Hilton
Ian Hilton
6,223 Points

Can somebody please explain this quiz question?

I can't find anywhere in the video where the answer to this question is covered... Can somebody please explain the following quiz question?

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 $_POST array, like this: $_POST["flavor"]. 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.)

Why does adding a name value to the "select" fix the form? I'm beyond confused.

(It's super frustrating to have to copy unexplained code from the video to pass the quiz. I had to guess at the answers to get past the question and I still don't understand the question or the answer to it...)

5 Answers

The php form processor needs to "understand" which of all elements you are referring to as "flavor". The <select> already has an attribute id=flavor but the php processor only checks the value of the name attribute, and this is what you need to add here.

<select name="flavor" id="flavor">

If you did not include a name attribute, your form would be sent with all the form-data (including what you selected in the drop-down), but if you use the variable $_POST['flavor'] in your php code, php won't know what you'd be referring to - it doesn't know of any flavor data. So adding name="flavor" in your HTML puts a label on that element and once the form is submitted, php will know what "flavor" is.

The other attribute you need to add is the form method. In this case it's post:

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

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Ian;

When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "process.php". The form data is sent with the HTTP POST method.

The name="flavor" attribute will allow process.php to process something like:

Your chosen flavor is: <?php echo $_POST["flavor"]; ?>

Similarly if you are doing a contact form with name, email, and phone numbers you would want something like:

contact_form.html

<html>
<body>

<form action="process.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Phone:<input type="text" name="phone"><br>
<input type="submit">
</form>

</body>
</html> 

And then you could access that information in process.php like:

process.php

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
Your phone number is: <?php echo $_POST["phone"]; ?><br>

</body>
</html> 

Obviously there is much more that can be done in PHP than take a variable, process it, and return it to the screen, but hopefully you get the idea the processing script needs those name attributes. Make sense?

Ken

Ian Hilton
Ian Hilton
6,223 Points

Thank you so much Christian, I really appreciate it!

Hi Ian,

When a form is submitted via the POST method (<form method="post">) the $_POST array uses the value of the name attribute (<select id="flavor" name="flavor">) as a key in the array. This key then points to the value selected in the option element.

/***** $_POST *****/

Array
{
  [flavor] => Vanilla
}
Ian Hilton
Ian Hilton
6,223 Points

Thanks Ken! I appreciate the answer and the time it took to create.