douxian3170 2016-10-05 04:47
浏览 42

使用json在db上插入多个复选框

i am using a form validation plugin and in my js file i have this for input and checkbox value pass to json

$('form').submit(function(event) {

    $('.form-group').removeClass('has-error'); // remove the error class
    $('.help-block').remove(); // remove the error text
    var chkbox = new Array();
    $('input:checked').each(function() {
        chkbox.push(parseint($(this).val()));
    });

    var formData = {
        'company_name': $('input[name=company_name]').val(),
    }

    $.ajax({
        type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url: 'process.php',
        data: formData,chbox,
        dataType: 'json',
        encode: true
    }).done(function(data) {
        console.log(data); 
        if ( ! data.success) {
            if (data.errors.company_name) {
                $('#company_name-group').addClass('has-error');
                $('#company_name-group').append('<div class="help-block">' + data.errors.company_name + '</div>');

Then in php

$errors         = array(); 
$data           = array(); 

                if (empty($_POST['company_name']))
                    $errors['company_name'] = 'Company Name is required.';
                if ( ! empty($errors)) {
                    // if there are items in our errors array, return those errors
                    $data['success'] = false;
                    $data['errors']  = $errors;
                } else {
                    $company_name=mysqli_real_escape_string($con,$_POST['company_name']);
                    $chkbox = $_POST['chkbox'];
                    $chkNew = "";  
                foreach($chkbox as $chkNew1)  
                {
                    $chkNew .= $chkNew1 . ",";  
                }  

                $ins_query="insert into xxx(`company_name`,`Job`)values('company_name','$chkNew');"
                retval = mysqli_query( $con ,  $ins_query);
                $data['success'] = true;
                $data['message'] = 'Success!';
            }
            echo json_encode($data);

This work perfectly fine for insert values of form on db values but it stops in my process.php file and doesnt show the message on page .When i remove chkbox from js file form is validated and shows message success in succesfull insertion to db .

  • 写回答

1条回答 默认 最新

  • duanleiliu7373 2016-10-07 08:42
    关注

    The last character in $chkNew is ,.

    You should remove it.

    Try to add this after foreach:

    $chkNew= trim($chkNew, ',');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)