soma56 Posted April 5, 2011 Share Posted April 5, 2011 I have a table that I must be populated based on a check box selection. The maximum selection is 5, however, there are more then 10 choices. For example: Here is a choice of 10 different (checkbox) options that the user can select from: 1. Coke 2. Root Beer 3. Pepsi 4. Dr. Pepper 5. Sprite 6. 7up 7. Cream Soda 8. Club Soda 9. Water 10. Milk I've already managed to limit the users selection to only 5. My issue is determining which of the checkboxese were actually selected so that I can correctly place them in my database. The first thing to do is create variables from all the checkboxes: $coke = $_POST['coke']; $rootbeer = $_POST['rootbeer']; $pepsi = $_POST['pepsi']; $drpepper = $_POST['drpepper']; etc. etc. Now that I have all 10 selections as variables from a submitted form how can I determine which ones are empty (or which ones have values) so that I can then save them to the database respectively? Quote Link to comment https://forums.phpfreaks.com/topic/232753-determine-which-_posts-has-values/ Share on other sites More sharing options...
dcro2 Posted April 5, 2011 Share Posted April 5, 2011 Technically you should be doing this: if(isset($_POST['coke'])) { //do something with it } Just make sure the checkboxes have a value. Quote Link to comment https://forums.phpfreaks.com/topic/232753-determine-which-_posts-has-values/#findComment-1197211 Share on other sites More sharing options...
Adam Posted April 5, 2011 Share Posted April 5, 2011 Personally I use empty - which implicitly performs an isset check too. Really useful function. Quote Link to comment https://forums.phpfreaks.com/topic/232753-determine-which-_posts-has-values/#findComment-1197212 Share on other sites More sharing options...
KevinM1 Posted April 5, 2011 Share Posted April 5, 2011 It's far simpler to just give your check boxes the same name with an array indicator, like so: <input type="checkbox" name="drinks[]" value="Coke">Coke</input> <input type="checkbox" name="drinks[]" value="Pepsi">Pepsi</input> You'd then get an array which is only filled with the checked values: foreach($_POST['drinks'] as $drink) { echo $drink; } Quote Link to comment https://forums.phpfreaks.com/topic/232753-determine-which-_posts-has-values/#findComment-1197213 Share on other sites More sharing options...
PFMaBiSmAd Posted April 5, 2011 Share Posted April 5, 2011 Or more generically - <?php $choices = array(0=>'Coke',1=>'Root Beer',2=>'Pepsi',3=>'Dr. Pepper',4=>'Sprite',5=>'7up',6=>'Cream Soda',7=>'Club Soda',8=>'Water',9=>'Milk'); echo "<form method='post' action=''>"; foreach($choices as $key => $value){ echo "<input type='checkbox' name='drinks[]' value='$key' />$value<br />\n"; } echo "<input type='submit'>"; echo "</form>"; // validate list of checkbox fields if(!isset($_POST['drinks'])){ echo "You must select at least one!"; } else { // at least one picked if(count($_POST['drinks']) > 5){ echo "You may pick a maximum of 5!"; } else { // a valid number picked echo "You picked - <br />"; foreach($_POST['drinks'] as $key){ // use the value here to form your query... echo "$choices[$key]<br />"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232753-determine-which-_posts-has-values/#findComment-1197224 Share on other sites More sharing options...
soma56 Posted April 5, 2011 Author Share Posted April 5, 2011 Brilliant. Thanks guys/girls. I ended up simply placing them into a new array and then called the first 5 values for the DB. foreach($_POST['service'] as $service) { $services[] = $service; } And then for the php/mysql insert it was simply a matter of using $service[0], $service[1], etc. It doesn't matter if they're blank or not - however if all 5 are selected then this will place them in an array and subsequently into the database. Thanks Again! Quote Link to comment https://forums.phpfreaks.com/topic/232753-determine-which-_posts-has-values/#findComment-1197473 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.