hi2you Posted February 29, 2008 Share Posted February 29, 2008 Hello, I have a list of check box <form name="form" action="test.php" method="POST"> <input type="checkbox" name="option1" value="a"> a<br> <input type="checkbox" name="option2" value="b" > b<br> <input type="checkbox" name="option3" value="c"> c<br> </form> and in test.php, I have a String varible $text, and if a and b are checked, then $text = "ab". if a, c are checked, then $text becomes "ac". How to do so?? Thanks!! Link to comment https://forums.phpfreaks.com/topic/93641-get-check-box-value/ Share on other sites More sharing options...
fooDigi Posted February 29, 2008 Share Posted February 29, 2008 try this, form page... <form name="form" action="test.php" method="POST"> <input type="checkbox" name="opt[]" value="a"> a <input type="checkbox" name="opt[]" value="b" > b <input type="checkbox" name="opt[]" value="c"> c <input type="submit" /> </form> test.php... <?php foreach($_POST['opt'] as $opt) $text .= $opt; echo $text; ?> Link to comment https://forums.phpfreaks.com/topic/93641-get-check-box-value/#findComment-479789 Share on other sites More sharing options...
hi2you Posted February 29, 2008 Author Share Posted February 29, 2008 Thanks, I see how it works now, but what if the box is unchecked and I dont want the value of the unchecked into $text?? your example will String up all checked as well as the unchecked values right? Link to comment https://forums.phpfreaks.com/topic/93641-get-check-box-value/#findComment-479791 Share on other sites More sharing options...
fooDigi Posted February 29, 2008 Share Posted February 29, 2008 it shouldn't, try it for me it worked Link to comment https://forums.phpfreaks.com/topic/93641-get-check-box-value/#findComment-479792 Share on other sites More sharing options...
thebadbad Posted February 29, 2008 Share Posted February 29, 2008 or <form action="test.php" method="post"> <input type="checkbox" name="option1" value="a" /> a <input type="checkbox" name="option2" value="b" /> b <input type="checkbox" name="option3" value="c" /> c <input type="submit" /> </form> test.php <?php $text = ''; for ($i = 1; $i <= 3; $i++) { $text .= $_POST['option'.$i]; } ?> To answer your question, unchecked checkboxes won't get submitted at all. Link to comment https://forums.phpfreaks.com/topic/93641-get-check-box-value/#findComment-479795 Share on other sites More sharing options...
hi2you Posted February 29, 2008 Author Share Posted February 29, 2008 Thanks guys! Link to comment https://forums.phpfreaks.com/topic/93641-get-check-box-value/#findComment-479796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.