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
Joshua Farley
11,312 PointsPHP Checkbox Form Items
PHP Checkbox form items are returning as "on" in email. At first, I was having difficulty getting the check boxes to implode to the email sending function, but now they are not showing the value for the check boxes. So, if I select multiple check boxes, it will return an array of strings that are all set to "on".
How do I get the PHP process to capture the values associated with the checkbox that is selected?
<li class="servNI">
<input type="checkbox" class="check" name="check[]" value"Large Screens">
<label for="test">Large Screens</label>
</li>
<li class="servNI">
<input type="checkbox" class="check" name="check[]" value"Lighting">
<label for="test">Lighting</label>
</li>
$email_body = $email_body . "Interested in: ".implode(" ", $_POST['check']) . "<br /><br />";
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Joshua,
Checked checkboxes will have a value of "on" if a value attribute isn't specified in the html.
You don't have an equal sign on your value attributes so I suspect that is your problem.
Jacob Herper
94,150 PointsI would simply give them unique names, at least that way they will return the set values.
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsThank you!! Sometimes I make the dumbest mistakes (usually because I am in a hurry)! However, this is not fixing the problem in the php.
I know that I need to implode the value instead of the status, but I am uncertain about how to do this.
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsHere are two snippets that I found online, but they are not seeming to work correctly.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHave you verified that you're getting the expected values now from the individual checkboxes?
What are you currently getting for $email_body?
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsWhen I get the email, I get: "Interested in: on on on on on" ( I have about 20 checkboxes that I am doing this with. I put 2 in this post for simplicity.) Basically, I had selected 5 different services for testing this form.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsOk, so it looks like your implode is working properly but you still have the problem with "on"
You put equals in your value attribute like this:
value="Large Screens"? And did you do it for all 20, save and refresh?I hate to state the obvious but better to make sure.
I'm working on a couple of test files right now to test this locally.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHere's check_test.html:
test.php
Output is:
Interested in: check1 check2If I remove the equal signs then I get:
Interested in: on onI think this is an accurate representation of your code.
My best guess at this point is that you still have a problem in your html if you're getting "on" in your php.
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsYes, that is the format that I have used for all of the list items in the checkbox series. I reloaded the page and tested it. I even changed the format of the spacing (in the email) from " " to " | " to make certain that the form wasn't pulling the process.php through the cache.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsIf process.php is the one containing the implode code then I don't think it would matter if that was cached or not because it wasn't changed.
If your html form is in a different file then it would matter if you were using a cached version of it.
Are you developing locally or on a remote server?
You could try the 2 files that I put up. At least they won't be cached and you'll know if the basic code is working.
Then we can try to spot what might be different with your code and my test setup.
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsMy apologies, I missed those previous comments. Let me look at this really quick
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsHere is a larger sampling of what I have:
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsI am uploading to a temporary website through a free hosting service. Every change that I am making is being uploaded through an FTP
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsThe cache was the problem! That completely fixed it! Thank you so much!!!!
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsTry clearing your cache then in case the form is cached.
Did you try yet to upload my 2 test files? Since these are new files it will eliminate any caching problems.
The "on" value is an html issue and is something that is created by the browser as it's getting the form ready for submission so I don't think that this can be a php problem. The browser is sending the php script that "on" value.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsSuccess! I'm glad you got it working because I was out of ideas. :)
Thank you for keeping it all in the comments too.
Joshua Farley
11,312 PointsJoshua Farley
11,312 PointsNot a problem! Thanks again Jason!