How do I insert multiple checkbox values into database? - php

How do I insert multiple checkbox values into database? Here is my code, it workes for the radio buttons where there is only one value, but how do I insert more if I have checkboxes?
PHP CODE:
if(isset($_POST['radio']) &&
isset($_POST['checkbox'])){
$radio = $_POST['radio'];
$checkbox= $_POST['checkbox'];
$sql = "INSERT INTO info(radio, checkbox)VALUES ('$radio', '$checkbox')";
$result = mysql_query($sql);
}
HTML CODE:
<input type="radio" name="radio" value="1">1<br>
<input type="radio" name="radio" value="2">2<br>
<input type="checkbox" name="checkbox" value="1">1<br>
<input type="checkbox" name="checkbox" value="2">2<br>
<input type="checkbox" name="checkbox" value="3">3<br>
<input type="checkbox" name="checkbox" value="4">4<br>
<input type="checkbox" name="checkbox" value="5">5<br>
<input type="checkbox" name="checkbox" value="6">6<br>

Html
<input type="radio" name="radio" value="1">1<br>
<input type="radio" name="radio" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="1">1<br>
<input type="checkbox" name="checkbox[]" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="3">3<br>
<input type="checkbox" name="checkbox[]" value="4">4<br>
<input type="checkbox" name="checkbox[]" value="5">5<br>
<input type="checkbox" name="checkbox[]" value="6">6<br>
Php
use a foreach to get all the values of checkboxes
//Do the necessary coding and then
if(!empty($_POST['checkbox']))
foreach($_POST['checkbox'] as $key=>$value){
if(trim($value) != ''){
//Do insertion here
}
}
}

change the names of yours checkboxes to:
<input type="checkbox" name="checkbox[]" value="1">1<br>
<input type="checkbox" name="checkbox[]" value="2">2<br>
<input type="checkbox" name="checkbox[]" value="3">3<br>
<input type="checkbox" name="checkbox[]" value="4">4<br>
<input type="checkbox" name="checkbox[]" value="5">5<br>
<input type="checkbox" name="checkbox[]" value="6">6<br>
on php do it:
$radio = $_POST['radio'];
$sql = 'INSERT INTO info(radio, checkbox)';
foreach($_POST['checkbox'] as $item){
$sql .= "VALUES('$radio', '$item')," ;
}
$sql = trim($sql, ','); //remove the last ',';
//performe all insert at once.
avoid to use mysql_* functions, try PDO

use php core functions serialize() and unserialize()

Related

Conditional PHP Statements relating to an HTML form

I have an HTML form that I need to then reference to in PHP, so that I can eventually filter through data. Right now it is just echoing a bit of text for testing purposes.
I use the $_GET variable to get which values are equal to what, then use an if/else statement to tell me if each is checked.
If I say it only equals one value (== .25) it returns false.
However if I add one more or both more values (== .25 or .375 or .5) it will return the value I need.
How can I get it to return true with only one value?
<table stlye="width:100%">
<tr>
<td style="width:50%">
<form method="GET">
Tool Diameter: <br>
<input type="checkbox" name="Tool Diameter" value=.25 checked> .25<br>
<input type="checkbox" name="Tool Diameter" value=.375 checked> 3/8<br>
<input type="checkbox" name="Tool Diameter" value=.5 checked> 1/2<br><br>
Brand: <br>
<input type="checkbox" name="Brand" value="Lakeshore Carbide " checked> Lakeshore Carbide<br>
<input type="checkbox" name="Brand" value="AB Tools" checked> AB Tools<br>
<input type="checkbox" name="Brand" value="Helical Tools" checked> Helical Tools<br><br>
Flutes: <br>
<input type="checkbox" name="Flutes" value="2" checked> 2<br>
<input type="checkbox" name="Flutes" value="3" checked> 3<br>
<input type="checkbox" name="Flutes" value="4" checked> 4<br><br>
Tool Material: <br>
<input type="checkbox" name="Material" value="HSS" checked> HSS<br>
<input type="checkbox" name="Material" value="Carbide" checked> Carbide<br>
<input type="checkbox" name="Material" value="Cobalt" checked> Cobalt<br><br>
Coating: <br>
<input type="checkbox" name="Coating" value="Uncoated" checked> Uncoated<br>
<input type="checkbox" name="Coating" value="ZrN" checked> ZrN<br>
<input type="checkbox" name="Coating" value="TiCN" checked> TiCN<br><br>
Tool Type: <br>
<input type="checkbox" name="Type" value="Face Mill" checked> Face Mill<br>
<input type="checkbox" name="Type" value="Flat Endmill" checked> Flat Endmill<br>
<input type="checkbox" name="Type" value="Ball Endmill" checked> Ball Endmill<br>
<br><button>Filter</button><br>
</form>
</td>
<td style="width:50%">
<style type="text/css">
td
{
padding:0 50px 0 50px;
}
</style>
<?php
//while (true){
if ($_GET['Tool Diameter'] == .375) {
echo 'test = true';
}
else {
echo "false";
}
?>
</td>
</tr>
</table>
Convert the values to strings e.g.
<input type="checkbox" name="Tool Diameter" value=".375" checked> .375<br>
then check
if ($_GET['Tool Diameter'] == ".375"){
enter code here
}
try this
<input type="checkbox" name="Tool Diameter[]" value=".25" checked> .25<br>
<input type="checkbox" name="Tool Diameter[]" value=".375" checked> 3/8<br>
<input type="checkbox" name="Tool Diameter[]" value=".5" checked> 1/2<br><br>
.....
<?php
// for checking the condition 'atleast 1 ' should be checked
if(sizeof($_GET['Tool Diameter']) >=1){
echo 'test = true';
}
else {
echo "test = false";
}
?>
1st mistake
values have to be wrapped in parenthesise value=".25"
2nd mistake
names have to be unique, as a result you have only one value in the array $_GET['Tool Diameter']
you should have a slot in $_GET that contains i.e. array of your results, so lets say
$_GET['Dimensions'] = [
'Dimension 1' => '.25',
'Dimension 2' => '.375',
'Dimension 3' => '.5'
];
and then refer to each of them separately

How to get values of multiple selected (dynamic) checkbox in php?

I'm a beginner.I have displayed student id and student name of 10 students on a table. Against each student id, there should be a checkbox(dynamic). When I click the ADD button, all the checked students details (id,name) must be inserted into another database table. What should i do?
Use checkbox name as an array,
example :
<form method="post" action="" id="frm_id">
<input type="checkbox" name="chkid[]" value="10,Anu" />Anu
<input type="checkbox" name="chkid[]" value="11,Raj" />Raj
<input type="checkbox" name="chkid[]" value="12,Ram" />Ram
<input type="checkbox" name="chkid[]" value="13,xxx" />xxx
<input type="checkbox" name="chkid[]" value="14,yyy" />yyyy
<input type="checkbox" name="chkid[]" value="15,zzz" />zzz
<input type="checkbox" name="chkid[]" value="16,qqqq" />qqqq
<input type="submit" value="Insert" name="sub"/>
</form>
<?php
if(isset($_POST['sub']))
{
$id=$_POST['chkid'];
for($i=0;$i<count($id);$i++)
{
$exp=explode(',',$id[$i]);//Explode id and name
echo 'id='.$exp[0].',Name='.$exp[1];echo "<br>";
echo $query="INSERT INTO tbl_student (id,name) values ('$exp[0]','$exp[1]')";echo "<br><br>";
}
}
?>
<form method="post" action="pageurl">
<input type="checkbox" name="studentid[]" value="1,Student1" />Student1
<input type="checkbox" name="studentid[]" value="2,Student2" />Student2
<input type="checkbox" name="studentid[]" value="3,Student3" />Student3
<input type="checkbox" name="studentid[]" value="4,Student4" />Student4
<input type="submit" />
</form>
<?php
$id=$_POST['studentid'];
foreach($id as $student)
{
$extract = explode(',',$student);
$query="INSERT INTO student (id,name) values ('$extract[0]','$extract[1]')";
}
?>
try using the array of checkbox element like this:
<input type="checkbox" name="months[]" value="feb">February<br>
<input type="checkbox" name="months[]" value="mar">March<br>
<input type="checkbox" name="months[]" value="apr">April<br>

Get values from Dynamically created textboxes and checkboxes linked to textbox

I want to know how to capture values of dynamically created textboxes and checkboxes linked to each other.
My HTML elements would be like this:
<div class="data_class">
<input class="textbox_class" name="textbox[]" type="text" />
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
</div>
The checkboxes are linked to input with type="text"(i.e textbox)
Through JQuery i can clone the HTML elements in the "data_class" and append into it.
But while posting, how can we get the checkbox values related to each textbox.
i.e if i create 5 such elements like:
<div class="data_class">
<input class="textbox_class" name="textbox[]" type="text" value="A"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="B"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="C"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="D"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
<input class="textbox_class" name="textbox[]" type="text" value="E"/>
<input class="checkbox_class" name="checkbox[]" type="checkbox" />1
<input class="checkbox_class" name="checkbox[]" type="checkbox" />2
<input class="checkbox_class" name="checkbox[]" type="checkbox" />3
<input class="checkbox_class" name="checkbox[]" type="checkbox" />4
<input class="checkbox_class" name="checkbox[]" type="checkbox" />5
</div>
How to get values that are checked for textbox with value "A", "B", "C", "D", "E" seperatly???
What I would do is to group them by sets, meaning:
One set given A:
A textbox, and 1 or more checkboxes:
So I would do it create a name="" attribute that would group them accordingly. Example:
This would be the initial markup:
<form method="POST">
<div class="data_class">
<div class="fieldset" data-group="A">
<input class="textbox_class" name="form_values[A][textbox]" type="text" />
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="1" />1
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="2" />2
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="3" />3
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="4" />4
<input class="checkbox_class" name="form_values[A][checkbox][]" type="checkbox" value="5" />5
</div>
</div>
<button type="button" id="spawn">Spawn More</button><br/>
<input type="submit" name="submit" />
</form>
Spawning other fieldsets:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#spawn').on('click', function(){
var $initial = $('div.fieldset:last').clone();
// or just increment a number instead
var next = String.fromCharCode($initial.attr('data-group').charCodeAt(0) + 1);
$initial.children('.textbox_class').attr('name', 'form_values['+next+'][textbox]');
$initial.children('.checkbox_class').attr('name', 'form_values['+next+'][checkbox][]');
$('.data_class').append('<div class="fieldset" data-group="'+next+'">'+$initial.html()+'</div>');
});
});
</script>
So in PHP, after submission, it would come up with something like this:
if(isset($_POST['submit'])) {
$values = $_POST['form_values'];
echo '<pre>';
print_r($values);
echo '</pre>';
}
Output:
Array
(
[A] => Array
(
[textbox] => test1
[checkbox] => Array
(
[0] => 1
[1] => 2
)
)
[B] => Array
(
[textbox] => test2
[checkbox] => Array
(
[0] => 4
[1] => 5
)
)
)
Sample Demo
Note: This is just an example, if you want a simple key, just use a numeric one instead.

multiple checkboxes submit url generator php

I have multiple checkboxes in my form and i want to use those to generate the url.
An example :
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox" value="a">
<input type="checkbox" name="checkbox" value="b">
<input type="checkbox" name="checkbox" value="c">
<input type="checkbox" name="checkbox" value="d">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
When the user presses the submit button he will have to be redirect to the generated url created as followed :
www.domain.com/checkbox/a+b+c+d
Depending on the choises of the user the url should be generated as above.
How can i pull this of?
In your checkbox.php, get all the checked checkboxes, create the url you want, and redirect the user to it with header("Location: $url").
HTML:
<input type="checkbox" name="checkbox[]" value="a" />
<input type="checkbox" name="checkbox[]" value="b" />
<input type="checkbox" name="checkbox[]" value="c" />
<input type="checkbox" name="checkbox[]" value="d" />
checkbox.php:
<?php
if ($_POST) {
$url = "http://www.domain.com/checkbox/";
$params = '';
$checkBoxes = $_POST['checkbox'];
foreach ($checkBoxes as $value) {
$params .= $value;
if ($value != $checkBoxes[count(checkBoxes) - 1])
$params .= '+';
}
$url .= $params;
}
header("Location: $url");
Here's a php example if you don't want to use AJAX
Top of your checkbox.php:
<?php
if (isset($_POST['checkbox'])){
$url = 'checkbox/' . implode('+',$_POST['checkbox']);
header('Location:'. $url);
die;
}
?>

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" />

Categories