Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/232753-determine-which-_posts-has-values/
Share on other sites

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;
}

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 />";
	}
}
}
?>

Brilliant. Thanks guys/girls.  :D

 

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.