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 trialJeff Lemay
14,268 PointsHow do I receive these form results?
I've been building a form where users select whether they want an item via checkbox and can add notes if they have any special requests. I've gone through "Build a Simple PHP Application" a couple times, but translating from how the contact form was made to what I need for this project has been difficult.
The mock-up can be viewed here: http://flatscreensigns.com/content-form.php
The list items are stored in associative arrays and are displayed using a foreach loop.
$brandings = array();
$brandings["VSS_br_welcome"] = array(
"title" => "Welcome Message",
"img" => "/img/VSS_br_welcome.jpg",
"select" => "on" <<(this is for pre-populating a checkbox because we recommend it)
);
<ul class="group grid12">
<?php foreach($brandings as $branding) { ?>
<li class="item grid3">
<h3 class="item_title"><?php echo $branding["title"]; ?></h3>
<img src="<?php echo $branding["img"]; ?>" alt="<?php echo $branding["title"]; ?>" class="item_img">
<div class="selector_cont">
<input type="checkbox" name="selector_<?php echo $branding["title"]; ?>" id="selector_<?php echo $branding["title"]; ?>" <?php if($branding["select"] == "on" ) {echo 'checked="checked"'; }?>>
<label for="selector_<?php echo $branding["title"]; ?>" id="selector">select</label>
</div>
<textarea name="notes_<?php echo $branding["title"]; ?>" id="notes_<?php echo $branding["title"]; ?>" class="notes"></textarea>
</li>
<?php } ?>
</ul>
I'd really appreciate some guidance on what is needed for receiving the results from the form. If an item is selected/checked, I'd like the results to include its title from the array, img from the array, and, if there are any, the notes associated.
I've tried a few things but the results I get are never what I expect. I don't think I understand how to interact with the arrays properly.
5 Answers
Nikolaos Papathanassopoulos
10,322 PointsFirst of all you are missing the "form"-tag:
<form
method="GET" //define how information gets send
action="pagename.php" //define where information is sent to.
>
//enter inputs checkboxes, textareas...
</form>
I dont have time to give you a detailed help instruction at the moment, ill check on this post later.
~ Nikos
Jeff Lemay
14,268 PointsI apologize, I do have a form tag in my code, I just didn't paste it into here. Now that you mention it though, why would I use "GET" as my method instead of "POST"?
Randy Hoyt
Treehouse Guest TeacherJ L,
I'd recommend you check out my two sets of videos on handling form submissions in PHP:
The contact form uses post
, but my next videos to be released with look at a get
form.
Jeff Lemay
14,268 PointsHI Randy,
I've been through the videos you suggested a couple times and I'm still confused. In the videos, you create variables for each item in the $_POST array and relate them to the form field.
$name = $_POST["name"];
$email= $_POST["email"];
$message= $_POST["message"];
But since my form fields are created using a foreach loop and associative arrays, how can I create variables for the $_POST array?
Randy Hoyt
Treehouse Guest TeacherWhen you first receive the post submission, put this code in place:
var_dump($_POST);
exit;
This will show you all the information about the $_POST array at that point, including all the elements it contains.
Does that help?