foxhill Posted September 17, 2009 Share Posted September 17, 2009 I am learning php, and I have a form with checkbox values. I need to create an array to hold these values, so I can later output them. How do I construct this? And then, how do I loop through the array in php to echo the values? Quote Link to comment https://forums.phpfreaks.com/topic/174596-checkbox-inputs-to-array/ Share on other sites More sharing options...
.josh Posted September 17, 2009 Share Posted September 17, 2009 In general, this is how it basically works: <?php echo "<pre>"; print_r($_POST); ?> <form action = '' method = 'post'> <input type = 'checkbox' name = 'something[]' value = '1' /> one <br/> <input type = 'checkbox' name = 'something[]' value = '2' /> two <br/> <input type = 'checkbox' name = 'something[]' value = '3' /> three <br/> <input type = 'submit' value = 'submt' name = 'submit' /> </form> Notice in the name attribute of the checkbox elements, you have a generic "something[]" for each one. If you check for instance value 1 and 3, you will get a 2 element array $_POST['something'][0] and $_POST['something'][1]. If you want to specifically keep track of which ones were tracked, you can explicitly name the array like so: <?php echo "<pre>"; print_r($_POST); ?> <form action = '' method = 'post'> <input type = 'checkbox' name = 'something[1]' value = '1' /> one <br/> <input type = 'checkbox' name = 'something[2]' value = '2' /> two <br/> <input type = 'checkbox' name = 'something[3]' value = '3' /> three <br/> <input type = 'submit' value = 'submt' name = 'submit' /> </form> This will cause all checked values to be posted to the specific elements. Quote Link to comment https://forums.phpfreaks.com/topic/174596-checkbox-inputs-to-array/#findComment-920150 Share on other sites More sharing options...
foxhill Posted September 17, 2009 Author Share Posted September 17, 2009 Okay, so if I explicitly name the array like you showed, will an unchecked box the a NULL value? Can I do something like: if $something[1] != NULL { echo $something[1]; }; Quote Link to comment https://forums.phpfreaks.com/topic/174596-checkbox-inputs-to-array/#findComment-920151 Share on other sites More sharing options...
.josh Posted September 17, 2009 Share Posted September 17, 2009 If you do this: <form action = '' method = 'post'> <input type = 'checkbox' name = 'something[1]' value = '1' /> one <br/> <input type = 'checkbox' name = 'something[2]' value = '2' /> two <br/> <input type = 'checkbox' name = 'something[3]' value = '3' /> three <br/> <input type = 'submit' value = 'submt' name = 'submit' /> </form> And you check 1 and 3, you will get this: $_POST['something'][1] => 1 $_POST['something'][3] => 3 So yes, you will be able to do for instance, this: if (isset($_POST['something'][2])) { ...} or whatever. Quote Link to comment https://forums.phpfreaks.com/topic/174596-checkbox-inputs-to-array/#findComment-920201 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.