ecopetition Posted November 23, 2008 Share Posted November 23, 2008 I have the following HTML inside a form: <input type="checkbox" name="multiple_choice[0]"/> <input type="checkbox" name="multiple_choice[1]"/> <input type="checkbox" name="multiple_choice[2]"/> <input type="checkbox" name="multiple_choice[3]"/> <input type="checkbox" name="multiple_choice[4]"/> On submitting the form, I need it so that it the PHP can tell whether the checkbox is on or off for each row. How can I do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/133900-checkboxes/ Share on other sites More sharing options...
wildteen88 Posted November 23, 2008 Share Posted November 23, 2008 Example: <?php if(isset($_POST['submit'])) { echo '<p><b>You chose:</b> ' . implode(', ', $_POST['multiple_choice']) . '</p>'; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?php $choices = array('Chocolate', 'Milk', 'Candy', 'Apple', 'Coca Cola'); echo 'What do you like?<br />'; foreach($choices as $key => $choice) { $chkd = (isset($_POST['multiple_choice'][$key])) ? 'checked="checked" ' : null; echo '<input type="checkbox" name="multiple_choice['.$key.']" value="'.$choice.'" '.$chkd.'/> '.$choice."<br />\n"; } ?> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/133900-checkboxes/#findComment-697015 Share on other sites More sharing options...
ecopetition Posted November 23, 2008 Author Share Posted November 23, 2008 Is there a script that can just return on or off for each entry, regardless of how many checkboxes there is? For example, [0] => on, [1] => on, [2] => off Quote Link to comment https://forums.phpfreaks.com/topic/133900-checkboxes/#findComment-697154 Share on other sites More sharing options...
wildteen88 Posted November 23, 2008 Share Posted November 23, 2008 Is there a script that can just return on or off for each entry, regardless of how many checkboxes there is? For example, [0] => on, [1] => on, [2] => off Yea, how I just showed you. There is no other way... other than: <input type="checkbox" name="multiple_choice[0]" <?php if(isset($_POST['multiple_choice'][0])) echo 'checked="checked" '; ?>/> <input type="checkbox" name="multiple_choice[1]" <?php if(isset($_POST['multiple_choice'][1])) echo 'checked="checked" '; ?>/> <input type="checkbox" name="multiple_choice[2]" <?php if(isset($_POST['multiple_choice'][2])) echo 'checked="checked" '; ?>/> <input type="checkbox" name="multiple_choice[3]" <?php if(isset($_POST['multiple_choice'][3])) echo 'checked="checked" '; ?>/> <input type="checkbox" name="multiple_choice[4]" <?php if(isset($_POST['multiple_choice'][4])) echo 'checked="checked" '; ?>/> But that is just very repetitive, Which is why I opted to use an array for generating my checkboxes in my example. <?php $choices = array('Chocolate', 'Milk', 'Candy', 'Apple', 'Coca Cola'); echo 'What do you like?<br />'; foreach($choices as $key => $choice) { $chkd = (isset($_POST['multiple_choice'][$key])) ? 'checked="checked" ' : null; echo '<input type="checkbox" name="multiple_choice['.$key.']" value="'.$choice.'" '.$chkd.'/> '.$choice."<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133900-checkboxes/#findComment-697224 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.