I have a table of checkbox and i want to save them in my database, I need to save them eachone with its name and its value and in each one as a recorde :
<tr><td><input class="perm" name="feature[]" type="checkbox" value="1" id="feat1"/>test1 </td>
<td><input class="perm" name="feature[]" type="checkbox" value="1" id="feat9"/>test2 Monats </td>
<td><input class="perm" name="feature[]" type="checkbox" value="1" id="feat17"/>test3m</td>
<td><input class="perm" name="feature[]" type="checkbox" value="1" id="feat25"/>test4</td>
</tr>
<tr class="visible-xs" aria-hidden="true">
<td> </td>
</tr>
<tr><td> <input class="perm" name="feature[]" type="checkbox" value="1" id="feat2"/>test5</td>
<td><input class="perm" name="feature[]" type="checkbox" value="1" id="feat10"/>test6n</td>
<td><input class="perm" name="feature[]" type="checkbox" value="1" id="feat18"/>test7</td>
<td><input class="perm" name="feature[]" type="checkbox" value="1" id="test8</td>
</tr>
and my PHP Code:
$checkBox[] = implode(',',$_GET['feature']);
for($i=0; $i<sizeof($checkBox ); $i++){
$tsql="INSERT INTO [dbo].[_PARAM](Value) VALUES ('" . $checkBox[$i] . "')";
$stmt = sqlsrv_query( $conn, $tsql);
}
But my Database has 2 columns Name , Value , ex. if the test1 and test2 checked
and I need the table to be like
Name Value
test1 1
test2 1
test3 null
test4 null
..... ....
I need all the test and only the value 1 for the checked ones.
You need to change the values otherwise $_GET['feature'] will return an array of 1's
<tr><td><input class="perm" name="feature[]" type="checkbox" value="1" id="feat1"/>test1 </td>
<td><input class="perm" name="feature[]" type="checkbox" value="2" id="feat9"/>test2 Monats </td>
<td><input class="perm" name="feature[]" type="checkbox" value="3" id="feat17"/>test3m</td>
<td><input class="perm" name="feature[]" type="checkbox" value="4" id="feat25"/>test4</td>
EDIT
input type=hidden value="1,2,3,4,..,n" name="available">
than in php
$available=explode($_GET['available']);
foreach($available as $element) {
$values[$element]=in_array($element,$_GET['feature']) ? 1 : 0;
}
Keep a hidden field for every checkbox with value '0' (zero) like this:
<tr><td><input class="perm" name="feature[]" type="hidden" value="0" /><input class="perm" name="feature[]" type="checkbox" value="1" id="feat1"/>test1 </td>
<td><input class="perm" name="feature[]" type="hidden" value="0" /><input class="perm" name="feature[]" type="checkbox" value="1" id="feat9"/>test2 Monats </td>
<td><input class="perm" name="feature[]" type="hidden" value="0" /><input class="perm" name="feature[]" type="checkbox" value="1" id="feat17"/>test3m</td>
<td><input class="perm" name="feature[]" type="hidden" value="0" /><input class="perm" name="feature[]" type="checkbox" value="1" id="feat25"/>test4</td>
$checkBox[] = implode(',',$_GET['feature']);
for($i=0; $i<sizeof($checkBox ); $i++){
if ($checkBox[$i] == 0) {
$checkBox[$i] = null;
}
$name = "test".($i+1);
$tsql="INSERT INTO [dbo].[_PARAM](Name, Value) VALUES ('".$name."', '" . $checkBox[$i] . "')";
$stmt = sqlsrv_query( $conn, $tsql);
}
If your checkbox static than you can use an array which consist on checkbox values.
Example:
HTML:
<form method="post" action="">
<tr><td><input class="perm" name="feature[]" type="checkbox" value="test1" id="feat1"/>test1</td>
<td><input class="perm" name="feature[]" type="checkbox" value="test2" id="feat9"/>test2</td>
<td><input class="perm" name="feature[]" type="checkbox" value="test3" id="feat17"/>test3</td>
<td><input class="perm" name="feature[]" type="checkbox" value="test4" id="feat25"/>test4</td>
</tr>
<input type="submit" name="submti">
</form>
PHP:
<?php
if(isset($_POST['submti'])){
// all checkbox values
$checkboxArr = array('test1','test2','test3','test4');
if(is_array($_POST['feature'])){
foreach ($checkboxArr as $value) {
// check if value exist in post
if(in_array($value, $_POST['feature'])){
$newArr[$value] = 1; // if exist
}
else{
$newArr[$value] = 0; // if not exist
}
}
}
echo "<pre>";
print_r($newArr);
}
?>
Result:
Array
(
[test1] => 1
[test2] => 1
[test3] => 0
[test4] => 0
)
Now you can use this array into your query as you need.
First of all you have to set the content of your td element (eg test1, test2) as the value of the checkbox.You will also need a hidden input field for each checkbox and an indicator in the value field (0 or 1) about wether the checkbox is selected or not which is going to be prefix to the rest of the value attribute. When a checkbox is not checked,the form will pass as a value the one of the hidden field,wheras when the checkbox is checked, the form will pass as value the value of the checkbox, overwriting the value of the hidden field.
<tr>
<td><input type="hidden" name="feature[0]" value="0test1" /><input class="perm" name="feature[0]" type="checkbox" value="1test1" id="feat1"/>test1 </td>
<td><input type="hidden" name="feature[1]" value="0test2 Monats" /><input class="perm" name="feature[1]" type="checkbox" value="1test2 Monats" id="feat9"/>test2 Monats </td>
<td><input type="hidden" name="feature[2]" value="0test3m" /><input class="perm" name="feature[2]" type="checkbox" value="1test3m" id="feat17"/>test3m</td>
<td><input type="hidden" name="feature[3]" value="0test4" /><input class="perm" name="feature[3]" type="checkbox" value="1test4" id="feat25"/>test4</td>
</tr>
<tr class="visible-xs" aria-hidden="true">
<td> </td>
</tr>
<tr><td><input type="hidden" name="feature[4]" value="0test5" /><input class="perm" name="feature[4]" type="checkbox" value="1test5" id="feat2"/>test5</td>
<td><input type="hidden" name="feature[5]" value="0test6n" /><input class="perm" name="feature[5]" type="checkbox" value="1test6n" id="feat10"/>test6n</td>
<td><input type="hidden" name="feature[6]" value="0test7" /><input class="perm" name="feature[6]" type="checkbox" value="1test7" id="feat18"/>test7</td>
<td><input type="hidden" name="feature[7]" value="0test8" /><input class="perm" name="feature[7]" type="checkbox" value="1test8" id="feat8"/>test8</td>
</tr>
And your php code should look like this:
foreach($_GET["feature"] as $field)
{
$name = substr($field, 1);
$value = substr($field, 0,1) == 1 ? 1 : null;
$tsql="INSERT INTO [dbo].[_PARAM](Name,Value) VALUES ('$name','$value')";
$stmt = sqlsrv_query( $conn, $tsql);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.