saving multiple checkbox to database in PHP - 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.

Related

How to Make Check box is checked

How can I make my check box is checked using PHP, when i visit the page later i want previously selected check boxes are checked
<input name="product[]" type="checkbox" value="1" />
Start Session as
<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION))
{
if($_SESSION["products"] != null)
{
$session_products = $_SESSION["products"];
}
}
?>
Change your code as follows
<input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>/>
You can add a checked.
Here an example:
<input name="product[]" type="checkbox" value="1" checked />
By the way, your value=1 seems to be wrong. Normally you use `value to distinguish items.
Example:
<input name="product[]" type="checkbox" value="dvd" checked />
<input name="product[]" type="checkbox" value="cd" />
to do something like this
with php
<input name="product[]" type="checkbox" value="1" <?php echo ($value =="1") ? "checked" : ""; ?> />
where $value is the value from database

insert checkbox values in array into database

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

dealing with 42 checkboxes in one form

I have 42 checkboxes in my form as im programming a page where the user is choosing their interests.
By naming all the checkboxes "interest", is there any way of making an array of values?
Example:
<input type="checkbox" name="interest" value="34" />
<input type="checkbox" name="interest" value="19" />
//values in array
$interestArray[0] = 34;
$interestArray[1] = 19;
You're looking for this?
<form method="POST" action="">
<input type="checkbox" name="interest[]" value="34" />
<input type="checkbox" name="interest[]" value="19" />
<input type="checkbox" name="interest[]" value="56" />
//values in array
$_POST['interest'][0] = 34;
$_POST['interest'][1] = 19;
$_POST['interest'][2] = 56;
Specifying an arrays key is optional in HTML. If you do not specify the keys, the array gets filled in the order the elements appear in the form.
From PHP manual: How do I create arrays in a HTML ?
Yes you can:
<input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br />
<input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br />
Then use the array:
<?php
$aDoor = $_POST['formDoor'];
.....
}
?>
Use PHP's array shortcut notation: name="interest[]". The [] tell PHP to treat multiple values as part of an array. Each checked checkbox will get its own entry in that array.

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.

Categories