带有ajax的多个复选框

时间:2016-06-05 18:20:23

标签: php jquery html ajax

我有多个复选框,已经像这样回复了html然后我可以选择多个复选框

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

然后在ajax我用这个

 $("#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');
    });

在php中我使用它将值插入db。包含复选框值的单元格是$ 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);

除了“作业”复选框值外,将插入所有其他值 谢谢

2 个答案:

答案 0 :(得分:0)

您正在尝试插入未定义的变量$ checkbox,而不是使用$ Job

答案 1 :(得分:0)

我通过这个工作了

 var chkbox = new Array(); $("input:checked").each(function() {   
 chkbox.push($(this).val()); });

然后在php

$Job= mysqli_real_escape_string($con,$_REQUEST['Job']);
$chkbox = $_POST['chkbox'];
$chkNew = "";  
foreach($chkbox as $chkNew1)  
{$chkNew .= $chkNew1 . ",";  
}