Get all values from checkboxes? [duplicate] - php

This question already has answers here:
Get $_POST from multiple checkboxes
(5 answers)
Closed 9 years ago.
Is there an easy way to get the values of multiple checkboxes and store them in the database?
<?php
if(isset($_POST['go'])){
$fruit = $_POST['fruit'].",";
echo $fruit;
// if you selected apple and grapefruit it would display apple,grapefruit
}
?>
<form method="post">
Select your favorite fruit:<br />
<input type="checkbox" name="fruit" value="apple" id="apple" /><label for="apple">Apple</label><br />
<input type="checkbox" name="fruit" value="pinapple" id="pinapple" /><label for="pinapple">Pinapple</label><br />
<input type="checkbox" name="fruit" value="grapefruit" id="grapefruit" /><label for="grapefruit">Grapefruit</label><br />
<input type="submit" name="go" />
</form>

If you give the checkboxes the same name, ending in [], the values are returned as an array.
<input type="checkbox" name="fruit[]" value="apple" />
<input type="checkbox" name="fruit[]" value="grapefruit" />
Then in PHP ...
if( isset($_POST['fruit']) && is_array($_POST['fruit']) ) {
foreach($_POST['fruit'] as $fruit) {
// eg. "I have a grapefruit!"
echo "I have a {$fruit}!";
// -- insert into database call might go here
}
// eg. "apple, grapefruit"
$fruitList = implode(', ', $_POST['fruit']);
// -- insert into database call (for fruitList) might go here.
}
PS. please forgive the obvious error, that this example will potentially shout "I have a apple" ... I didn't think to make the example smart enough to determine when to use "a", and when to use "an" :P

Name your input like this :
<input type="checkbox" name="fruit[]" value="apple" id="apple" /><label for="apple">Apple</label><br />
<input type="checkbox" name="fruit[]" value="pinapple" id="pinapple" /><label for="pinapple">Pinapple</label><br />
Then iterate on the $_POST['fruit'] :
if(isset($_POST['fruit']) && !empty($_POST['fruit']))
foreach($_POST['fruit'] as $fruit) echo $fruit;

To add on to the other solutions...
implode and explode can be used in PHP to convert your array of fruit[] into a comma seperated list.
$valueToStoreInDB = implode(",",$_POST['fruit']);
$fruitAsAnArrayAgain[] = explode(",",$dbvalue);
Once you have your comma separated list, a good data type to represent the checkboxes in MySQL would be SET, and you can paste your comma seperated list right into your insert statement.

Related

PHP understanding a small snippet code for extracting checkbox values

I have a snippet of PHP code, it works but I was curious to know why.
Green: <input type="checkbox" name="color[]" value="green" />
Black: <input type="checkbox" name="color[]" value="black" />
White: <input type="checkbox" name="color[]" value="white" />
Blue: <input type="checkbox" name="color[]" value="blue" />
Red: <input type="checkbox" name="color[]" value="red" />
then here is what I don't get for this PHP code below, how is $colors an array, I thought you have to at least have $colors[] = $_POST['color'].
if (isset($_POST['color'])) {
$colors = $_POST['color'];
}
echo $colors[0];
Thanks for helping me better understand this.
In HTML, every element with [] in its name is gonna be treated as an array.
Useful in cases like:
Choose your interests<br/>
<input type='checkbox' name='interests[]' value='Fashion'> Fashion
<input type='checkbox' name='interests[]' value='Cars'> Cars
<input type='checkbox' name='interests[]' value='Health'> Health
<input type='checkbox' name='interests[]' value='Programming'> Programming
Now you will acces them as an array in php:
$_POST['interests'][0] //-> Fashion
You don't need to explicitly add [] in PHP. Notice that you don't even need to define the type! This is PHP :)
$_POST['color'] is an array, so $colors will also be!
Read: http://www.html-form-guide.com/php-form/php-form-checkbox.html
Edit
In PHP, if you write $colors[] = $var, you are actually adding $var to the end of that array. If the array is empty, $colors[0] == $var.
$colors = $_POST['color']; // $colors receives the array
$colors[] = $_POST['color']; // $colors[0] receives the array, if $colors was empty
More info: http://php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying

How to get the checkbox values in PHP if they all have the same name [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Adding multiple html checkboxes with the same name to URL using $_GET
I have the following code:
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('foo[]');
for(var i in checkboxes)
checkboxes[i].checked = source.checked;
}
</script>
<input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
<input type="checkbox" name="foo[]" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo[]" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo[]" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo[]" value="bar4"> Bar 4<br/>
I just want to ask how will I able to get all the values on the checkboxes that are checked using PHP?
I'm not sure I'm understanding you but here's my impression:
You need to use an input array:
<input type="checkbox" name="foo[]" value="bar1"> Bar 1<br/>
<input type="checkbox" name="foo[]" value="bar2"> Bar 2<br/>
<input type="checkbox" name="foo[]" value="bar3"> Bar 3<br/>
<input type="checkbox" name="foo[]" value="bar4"> Bar 4<br/>
Otherwise you're just assigning the last occurrence that is valid as the posted input.
If you want to process multiple boxes give each one a name ending with "[]" to create an array.
Only the values of the checked boxes will be sent to the php script:
// $_REQUEST['foo'] will return an array of the values
echo join(',', $_REQUEST['foo']);

checkbox array only returning one result

I have built a form that has a checkbox input array (saving to an array). However, when I POST it and retrieve the results, it only offers the last selection.
<input type="checkbox" value="Friendly" name="quest[9]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[9]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[9]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[9]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[9]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[9]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
For example, say I chose "Friendly", "Efficient", and "Genuine".
When I POST it over to a PHP document and run
print_r($_POST['quest']);
I only receive
Array ( [9] => Genuine )
back so "Genuine" is the only item I get back. Is there a way to fix this? What have I done wrong?
This is the 9th question on the survey, so changing the name unfortunately is not an option. Is there any way to combine the results to that single array separated by commas? I could always explode on the php side.
All your checkboxes have the same name, make them unique
<input type="checkbox" value="Friendly" name="quest[3]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[4]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[5]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[6]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[7]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[8]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
or use empty square brackets so php will treat the inputs as an array
<input type="checkbox" value="Friendly" name="quest[]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[]"> Genuine<br>
use quest[] in name instead of quest[9].
also in php part use this to add multiple choices .
<?php
$quest = implode(',',$_post['quest']);
print_r($quest);
?>
Happy coding!!
I'm posting a new answer about your comments on the previous one:
Since you must keep quest[9] as the organization for the checkbox array..
You may want to try and make it a more complex array, where each <input> has name="quest[9][1]", name="quest[9][2]" and so on.
And find the contents by
print_r($_POST['quest']);
again

Combine arrays depending on checked boxes

I have a form with check boxes.
I want it so that when a check box is checked, it includes an array.
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
If main is checked include the array 'main', if art is checked include the array 'art', etc.
I've tried, but I can't find a function that would work for this scenario.
Edit: I'm cheating a bit and am now doing it like so.
foreach($_GET as $get) {
$end = array_merge($end, $$get);
}
From your information it sounds like you want to merge an array depending on which checkboxes have been ticked? Am I correct in assuming this?
Is something like this what you are looking for?
<?php
$combinationArray = array();
$mainArray = array('item1','item2','item3');
$artArray = array('item4','item5','item6');
$gamesArray = array('item7','item8','item9');
if(isset($_POST['main']) && $_POST['main']=='main'){
$combinationArray = array_merge($combinationArray,$mainArray);
}
if(isset($_POST['art']) && $_POST['art']=='art'){
$combinationArray = array_merge($combinationArray,$artArray);
}
if(isset($_POST['games']) && $_POST['games']=='games'){
$combinationArray = array_merge($combinationArray,$gamesArray);
}
?>
HTML:
<form action="yourpage.php" method="post">
<input type="checkbox" name="main" value="main" checked> Main/unsorted<br />
<input type="checkbox" name="art" value="art" checked> Art/literature/music<br />
<input type="checkbox" name="games" value="games" checked> Games/gaming<br />
<button>
Submit
</button>
</form>

combine checkbox values using php mysql

I having around 20 check boxes in my form as
<input type="checkbox" name="c1" value="1" />
<input type="checkbox" name="c2" value="2" />
<input type="checkbox" name="c3" value="3" />
<input type="checkbox" name="c4" value="4" />
i would like to these values to a database. I just thought rather than creating 20 fields in my database grab all the values at store in the db as 1,2,3,4 and then when querying to explode the values and display it accordingly using php.
can someone please tell me how am i supposed to concatenate the values 1,2,3,4 from the check fields when submitted so i can pass to the database.
i would appreciate if anyone can suggest a different effective way to achieve this using php.
You can change the name of the checkboxes to be the same, something like
<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />
Then access them via $_GET or $_POST via
if (isset($_POST['cb'])) {
$my_values = implode(",", $_POST['cb']);
}
If you want them sorted, then you will want to do something like this:
if (isset($_POST['cb'])) {
$my_values = $_POST['cb'];
sort($my_values);
$my_db_value = implode(',', $my_values);
}
For the record, I agree with #Shef in the case that the database can handle the extra load. Depending on when this information will be needed in a highly scalable solution, however, this is a perfectly acceptable way to handle this.
To answer your initial question, first off you need to name your checkboxes all the same name, so:
<input type="checkbox" name="box[]" value="1" />
....
<input type="checkbox" name="box[]" value="20" />
PHP script:
$boxes = $_POST['box'];
$values = implode(",", $boxes); // $values now has "1,2,3,4...", insert into table
A better way would be to have a separate table (see #Shef 's comment).

Categories