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 trialYIHENG CHIU
2,761 PointsI 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
Treehouse Moderator 11,077 PointsFirst 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="">— Select —</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="">— Select —</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="">— Select —</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
2,761 PointsYIHENG CHIU
2,761 PointsThank you very much for the clear explanation!!!!
Oliver Goss
16,027 PointsOliver Goss
16,027 PointsThank you very much this has helped me with the challenge!
Dane Parchment
Treehouse Moderator 11,077 PointsDane Parchment
Treehouse Moderator 11,077 PointsGlad to have been of assistance to you both :D !
Darryl Mack
7,222 PointsDarryl Mack
7,222 PointsThank you very much! Great Explanation