I have some problem here where I could not get a single value from my multiple checkbox. I want to stored it in different column for each tick value=1, the box does not tick is 0 in database.For normal input I get the value but does not work on checkbox. Could you guys help.
HTML Code: This my html code
<div class="col-sm-4">
<div class="form-group">
<label for="colour">Machine</label><br>
<input type="text" id="input_1">
</div><br>
<input type="checkbox" name="Inspection[]" value="true"> Function<br>
<input type="checkbox" name="Inspection[]" value="true"> Visual<br>
<input type="checkbox" name="Inspection[]" value="true"> Sleeve<br>
<input type="checkbox" name="Inspection[]" value="true"> Mail<br>
</div>
<p><input type="button" id="add_btn" value="Save"></p>
JavaScript: Where I should get the value and store it in database.
$(document).on("click", "input#add_btn", function() { Addsave() });
function Addsave()
{
var Inspec1 = $("#input_1").val();
var Inspec2 = $("input[name='Inspection']:checked").val();
var Inspec3 = $("input[name='Inspection']:checked").val();
var Inspec4 = $("input[name='Inspection']:checked").val();
var Inspec5 = $("input[name='Inspection']:checked").val();
var tables = ["example"];
var AddObject = new Object();
AddObject.column1 = Inspec1;
AddObject.column2 = Inspec2;
AddObject.column3 = Inspec3;
AddObject.column4 = Inspec4;
AddObject.column5 = Inspec5;
var list = [AddObject];
var objParams = JSON.stringify(list);
var render = function(){ renderAddExec(data);};
insertExecute(list,tables,render);
}
Yesterday I found this in here stackoverflow but everytime I try, it does not work.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$checkBox = $_POST['Inspection'];
if(isset($_POST['add_btn']))
{
for ($i=0; $i<sizeof($checkBox); $i++)
{
$query="INSERT INTO example (column1,column2,column3,column4) VALUES ('" . $checkBox[$i] . "')";
mysql_query($query) or die (mysql_error() );
}
echo "Complete";
}
?>
mysql_*
functions have been deprecated since 2013 (in PHP 5.5), and are removed as of PHP 7 (released in 2015). This is because they have serious security vulnerabilities. DO NOT USE THEM. Please consider upgrading your PHP and switching to either MySQLi or PDO instead, ensuring that you also use prepared statements to prevent SQL injection. – Obsidian Age Dec 3 at 1:24name
isInspection
, and yet you are checking for a$_POST
ofDays
, which is never set. – Obsidian Age Dec 3 at 1:25Days
is because I do some testing a lot with using simple form to test it but even the simple one could get the input maybe @Obsidian is correct I will try another way using PDO perhaps. – Ubi_Rebus23 Dec 3 at 1:48true
because I want the value insert to different column of table each checkbox have their own column they are not mean to be in same column. – Ubi_Rebus23 Dec 3 at 1:51