dongqiangse6623 2016-06-05 18:20
浏览 65
已采纳

带有ajax的多个复选框

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

  • 写回答

2条回答 默认 最新

  • donvo24600 2016-06-06 14:07
    关注

    I got it working by this

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

    and then in php

    $Job= mysqli_real_escape_string($con,$_REQUEST['Job']);
    $chkbox = $_POST['chkbox'];
    $chkNew = "";  
    foreach($chkbox as $chkNew1)  
    {$chkNew .= $chkNew1 . ",";  
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化