i have multiple checkboxes that the've been echoed to html like this and then i can select more than one of them
<?php $got=mysqli_query($con, "SELECT * FROM JobChoose"); $checkbox =
''; while($row = mysqli_fetch_assoc($got)) { $checkbox .=
'<li><input type="checkbox" id="chkbox" name="chkbox[]" value =
"'.$row['Job'].'">'.$row['Job'].'</input></li>'; }
?>
<ul> <?php echo $checkbox;?> <li><input type="checkbox" id="check"
name="chkbox[]" value ="else">else</input></li>
Then in ajax i use this
$("#registerwth").click(function() {
var surname = $("#search_text").val();
var name = $("#name").val();
var custId = $("#custId").val();
var company_name = $("#company_name").val();
var firm = $("#firm").val();
var address = $("#address").val();
var town = $("#town").val();
var country = $("#country").val();
var telephone = $("#telephone").val();
var fax = $("#fax").val();
var mobile = $("#mobile").val();
var mail = $("#mail").val();
var web_site = $("#web_site").val();
var barcode = $("#barcode").val();
var checkbox = []
$("input[name='chkbox[]']:checked").each(function ()
{
checkbox.push(parseInt($(this).val()));
});
$.ajax({
url: "insertnew.php",
method: "POST",
data: {surname:surname,name:name,custId:custId,company_name:company_name,firm:firm,address:address,town:town,country:country,telephone:telephone,fax:fax,mobile:mobile,mail:mail,web_site:web_site,barcode:barcode,checkbox:checkbox} ,
dataType:"text",
success: function(data)
{
}
});
$(':input[type="text"]').val('');
$('input:checkbox').removeAttr('checked');
});
In php i use this to insert the values to db. The cell that contains the checkbox values is $Job
<?php
require('config.php');
include("auth.php");
date_default_timezone_set('Europe/Athens');
if(! $con ) {
die('Could not connect: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
}
$custId=mysqli_real_escape_string($con,$_POST['custId']);
$surname=mysqli_real_escape_string($con,$_POST['surname']);
$name= mysqli_real_escape_string($con,$_POST['name']);
$company_name=mysqli_real_escape_string($con,$_POST['company_name']);
$firm=mysqli_real_escape_string($con,$_POST['firm']);
$address= mysqli_real_escape_string($con,$_POST['address']);
$town=mysqli_real_escape_string($con,$_POST['town']);
$country=mysqli_real_escape_string($con,$_POST['country']);
$telephone= mysqli_real_escape_string($con,$_POST['telephone']);
$fax=mysqli_real_escape_string($con,$_POST['fax']);
$mobile=mysqli_real_escape_string($con,$_POST['mobile']);
$mail= mysqli_real_escape_string($con,$_POST['mail']);
$web_site=mysqli_real_escape_string($con,$_POST['web_site']);
$barcode=mysqli_real_escape_string($con,$_POST['barcode']);
$Job=mysqli_real_escape_string($con,$_POST['checkbox']);
$ins_query2="insert into base (surname,name,custId,company_name,firm,address,town,country,telephone,fax,mobile,mail,web_site,barcode,Job)
values ('".$surname."','".$name."','".$custId."','".$company_name."','".$firm."','".$address."','".$town."','".$country."','".$telephone."','".$fax."','".$mobile."','".$mail."','".$web_site."','".$barcode."','".$checkbox."')";
$retval = mysqli_query( $con , $ins_query2);
All the other values are inserted except Job checkbox value Thank you