insert checkbox values in array into database - php

I have 3 checkboxes :
<input class="shoesChoice" name="trendy" type="checkbox" />
<input class="shoesChoice" name="luxury" type="checkbox" />
<input class="shoesChoice" name="sports" type="checkbox" />
And I want to store this information into (if possible) only one field in my database.
So, if the 1rst and the last input are checked, my shoesChoice field should have : "trendy, sports"

Try to change the name and keep them in value
<input class="shoesChoice" name="shoes[]" value="trendy" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="luxury" type="checkbox" />
<input class="shoesChoice" name="shoes[]" value="sports" type="checkbox" />
In PHP, try to get values in this way:
<?php
if(isset($_REQUEST['shoes']))
{
$shoes = '';
foreach
}
?>

Related

PHP how to insert unchecked checkbox value [duplicate]

This question already has answers here:
POST unchecked HTML checkboxes
(44 answers)
Closed 1 year ago.
Need help. i have 5 checkboxes[] 3 is checked 2 unchecked
i want to insert all checkbox values but if checkbox is checked than insert value 1 and if unchecked insert 0
<input id="answer" type="text" name="answer[]" /> //checked
<input id="answer" type="text" name="answer[]" />
<input id="answer" type="text" name="answer[]" /> /checked
<input id="answer" type="text" name="answer[]" />
<input id="answer" type="text" name="answer[]" /> //checked
if i check the length or size of field than it displays and insert only checked items but i want to insert unchecked also with value 0
The browser does not send unchecked checkbox data to the server, PHP in this case.
So in your PHP you have to know about all the checkboxes that exist on your form and check for the existance of each to decide what to store to your database.
So something like
HTML
<input type="checkbox" name="cb1" value="1" /> //checked
<input type="checkbox" name="cb2" value="2" />
<input type="checkbox" name="cb3" value="3" /> /checked
<input type="checkbox" name="cb4" value="4" />
<input type="checkbox" name="cb5" value="5" /> //checked
if (isset($POST['cb1'])) {
$cb1 = $POST['cb1'];
}else{
// it was not checked because it was not sent
// so use the default or whatever UNCHECKED value when storing to the DB
$cb1 = 0; // for example
}
You can shorten that code a bit by doing
$cb1 = isset($POST['cb1']) ? $POST['cb1'] : 0;

saving multiple checkbox to database in PHP

i am having problem in printing the values of the different checkbox with the same name & different values..
PHP
//$infect_type=array();
$infect_type = isset($_POST['infect_type']) ? $_POST['infect_type'] : null;
$values= implode(",",$infect_type);
print_r($values);
HTML
<input type="checkbox" name="infect_type" value="Blood Born" />
<input type="checkbox" name="infect_type" value="Air Born" />
i can only get the value which is selected last before submitting.
Use array notation for checkboxes names:
<input type="checkbox" name="infect_type[]" value="Blood Born" />
<input type="checkbox" name="infect_type[]" value="Air Born" />
In this case $_POST['infect_type'] is going to be an array of checked values.
HTML
<input type="checkbox" name="infect_type[]" value="Blood Born" />
<input type="checkbox" name="infect_type[]" value="Air Born" />
PHP
<?php
$infect_type = $_POST['infect_type'];
foreach ($infect_type as $i) {
echo $i;
//Change the code here
}
?>
This will do.

Process HTML checkboxes and their values

How can I process the checkboxes only if they're checked and grab the value of the checked ones only.
php
if (is_array($_POST['add'])) {
foreach ($_POST['add'] as $key => $value) {
$_POST['add'][$key] = mysql_real_escape_string(stripslashes($value));
}
}
html
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[wmeet]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[wmeet]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[wmeet]"
class="checkbox" />
Only checked checkboxes are ever presented to PHP, so your PHP code is correct.
However, your HTML isn't correct, as all your checkboxes have the same name. This means PHP will only ever see one of them.
To get an array of checkboxes you either need to give your checkboxes unique names like this
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[ce]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[sf]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[sm]"
class="checkbox" />
Or use the empty box technique like this.
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[]"
class="checkbox" />

How to handle multiple checkboxes in a PHP form?

I have multiple checkboxes on my form:
<input type="checkbox" name="animal" value="Cat" />
<input type="checkbox" name="animal" value="Dog" />
<input type="checkbox" name="animal" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['submit']) {
echo $_POST['animal'];
}
I get "Bear", i.e. the last chosen checkbox value even though I picked all three. How to get all 3?
See the changes I have made in the name:
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
you have to set it up as array.
print_r($_POST['animal']);
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo $animal;
}
}
use square brackets following the field name
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
On the PHP side, you can treat it like any other array.

how i can select multiple values from check box?

hi i have multiple option in check box and when visitor or customer select multiple option then how i can get multiple values? plz explain with code thanks
Name the checkboxes with [] (or PHP will drop all but one of them (I don't recall if it is the first or last)).
<input type=checkbox name="foo[]" value="some value">
Then they will be accessible as an array in the $_GET or $_POST superglobal.
$_GET['foo'][]
Basically, set all the name tags to be the same for all your checkboxes (with []). Then in your script, the values will be available as an array
Html:
<input type="checkbox" name="tags[]" value="1" />
<input type="checkbox" name="tags[]" value="2" />
<input type="checkbox" name="tags[]" value="3" />
<input type="checkbox" name="tags[]" value="4" />
PHP:
print_r($_REQUEST['tags']);
Reference: http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html
Like this
<input type="checkbox" name="foo[]" value="bar" />
<input type="checkbox" name="foo[]" value="baz" />
<input type="checkbox" name="foo[]" value="qux" />
<?php
print_r($_POST['foo']);

Categories