sanchez77 Posted August 16, 2010 Share Posted August 16, 2010 Hey Everyone, been a while. I need some help with an easy one. I'm brain farting and blanking out. Someone please help. HTML form that has checkboxes: <form ...> <input name="check1" type="checkbox" value="Value 1"> </form> PHP Code $post_check1= $_POST["check1"]; $body .= "check1: ".$post_check1."\n"; The form doesn't return the value when checked. What I am missing? I know it's something simple. Please help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2010 Share Posted August 16, 2010 There's at least 6+ different things that could be wrong with your form or your php code. You would need to post all the form and the php code from the start of the file up through the $body .= "check1: ".$post_check1."\n"; statement for anyone to have a chance at helping you. Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/#findComment-1099917 Share on other sites More sharing options...
wildteen88 Posted August 16, 2010 Share Posted August 16, 2010 The snippets you have posted are fine. PHP will only be able to retrieve the values from the form once it is submitted, you do have a submit button? Maybe you need to show more code. Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/#findComment-1099918 Share on other sites More sharing options...
sanchez77 Posted August 16, 2010 Author Share Posted August 16, 2010 Here is the HTML Form: <form method=post action="pass.php"> <table align="center"> <tr><td colspan="5">What kind of color would you like?</td></tr> <tr align="center"> <td align="center">Black</td> <td align="center">White</td> <td align="center">Blue</td> <td align="center">Red</td> <td align="center">Green</td> </tr> <tr align="center"> <td align="center"><input name="checkbox1" type="checkbox" value="Black"></td> <td align="center"><input name="checkbox2" type="checkbox" value="White"></td> <td align="center"><input name="checkbox3" type="checkbox" value="Blue"></td> <td align="center"><input name="checkbox4" type="checkbox" value="Red"></td> <td align="center"><input name="checkbox5" type="checkbox" value="Green"></td> </tr> <td colspan="5"><div align="center"> <input type="submit" value="Send Color Choice" /> <input type="reset" name="Reset" value="Clear Form"> </div></td> </tr> </table> <input type="hidden" name="next_url" value="size.htm"> <input type="hidden" name="subject" value="Color Choice"> </form> Here is the PHP form that processes the Form. <?php $post_checkbox1 = $_POST["checkbox1"]; $post_checkbox2 = $_POST["checkbox2"]; $post_checkbox3 = $_POST["checkbox3"]; $post_checkbox4 = $_POST["checkbox4"]; $post_checkbox5 = $_POST["checkbox5"]; $body .= "Black: ".$post_checkbox1."\n"; $body .= "White: ".$post_checkbox2."\n"; $body .= "Blue: ".$post_checkbox3."\n"; $body .= "Red: ".$post_checkbox4."\n"; $body .= "Green: ".$post_checkbox5."\n"; $body .="\n"; mail( "email@domain.com", "Color Choice", $body, $message, "From: <email@domain.com>"); ?> Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/#findComment-1099926 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2010 Share Posted August 16, 2010 The form doesn't return the value when checked. The code you posted does. BTW, your mail() function call is incorrect. You have an extra parameter in the list. Perhaps if you posted the actual code that has the problem and actually told us what problem you are having with it? Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/#findComment-1099931 Share on other sites More sharing options...
sanchez77 Posted August 16, 2010 Author Share Posted August 16, 2010 That is the actually code. When I process the form, If I check the box or don't check the box, the output in the email is the same, nothing. I shouldn't say nothing, if I check black, the output in the email is "Black: ", if I don't check it, the output is "Black: " I want a user to be able to check black and the email output is "Black: Yes" How do you post a check or unchecked value in an email? Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/#findComment-1099937 Share on other sites More sharing options...
wildteen88 Posted August 16, 2010 Share Posted August 16, 2010 A checkbox will only send its value if its is checked. The code you have posted is the correct way for retrieving the value from checkboxes. You will not be able to get the value from an unchecked checkbox Add the following to your code echo "<pre>" . print_r($_POST, true) . "</pre>"; Submit your form with the checkboxes checked and post the output here. Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/#findComment-1099940 Share on other sites More sharing options...
sanchez77 Posted August 16, 2010 Author Share Posted August 16, 2010 Cool, so this is the output: Array ( [checkbox1] => Black [checkbox2] => White [subject] => Color Choice ) Now how do put that in the email that i'm sending. Right now, it prints on the screen. Quote Link to comment https://forums.phpfreaks.com/topic/210881-checkboxes/#findComment-1099946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.