在PHP + AJAX中插入多个复选框值

时间:2014-04-08 21:01:34

标签: php

我正在尝试使用ajax将多个复选框值插入数据库,但我不知道该怎么办这个方法。我尝试使用这些代码。

        <h3 class="page-header">International Language</h3>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">Chinese</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="chinese" name="chk[]" value="chinees">
                            </label>
                          </div>
                         </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">English</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="english" name="chk[]" value="english">
                            </label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">English, Welsh</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="englishwelsh" name="chk[]" value="englishwelsh">
                            </label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">Estonian</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="estonian" name="chk[]" value="estonian">
                            </label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">French</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="french" name="chk[]" value="frnch">
                            </label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">Greek</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="greek" name="chk[]" value="greek">
                            </label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">Latvia</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="latvia" name="chk[]" value="latvia">
                            </label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">Malay</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="malay" name="chk[]" value="malay">
                            </label>
                          </div>
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">Maltese</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="maltese" name="chk[]" value="maltese">
                            </label>
                          </div>

                        </div>
                      </div>
                      <div class="form-group">
                        <label class="col-md-3 control-label" for="">Swedish</label>
                        <div class="col-md-7">
                          <div class="checkbox checkbox-inline">
                            <label>
                              <input type="checkbox" id="swedish" name="chk[]" value="swedish">
                            </label>
                          </div>
                        </div>
                      </div>



    $('#btn_ielts_assesment_next').click(function()
        {   
            var id =$("#hlast_id").val();
            var joboffered=$('#joboffered').val();
            var givenielts=$('#givenielts').val();
            var ieltsband=$('#ieltsband').val();
            var ieltspassingyear=$('#ieltspassingyear').val();
            var chiness=$('#chinese').val();
            var english=$('#english').val();
            var englishwelsh=$('#englishwelsh').val();
            var estonian=$('#estonian').val();
            var french=$('#french').val();
            var greek=$('#greek').val();
            var latvia=$('#latvia').val();
            var malay=$('#malay').val();
            var maltese=$('#maltese').val();
            var swedish=$('#swedish').val();
            $.post('<?php echo SERVER_ADMIN_URL;?>ajax/ielts_ajax.php',{action:"btn_ielts_assesment_next",hlast_id:id,                                          joboffered:joboffered,givenielts:givenielts,ieltsband:ieltsband,ieltspassingyear:ieltspassingyear,chiness:chiness,
            english:english,englishwelsh:englishwelsh,estonian:estonian,french:french,greek:greek,latvia:latvia,malay:malay,maltese:maltese        ,swedish:swedish},function(data)
            {

            });
        });

<php>

<?php
if($_POST['action'] == 'btn_ielts_assesment_next'){
$table='assessment';
//$lang=();
$language=implode(",");
$set2=array(
            'joboffered'=>$_POST['joboffered'],
            'givenielts'=>$_POST['givenielts'],
            'ieltsband'=>$_POST['ieltsband'],
            'ieltspassingyear' => $_POST['ieltspassingyear'],
            'languageknown'=>$language);
            $main_where=array('id'=>$_POST['hlast_id']);
            $obj_conn->Update($table,$set2,$main_where, $exclude = '');


}


?><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
</body>
</html>
你可以帮帮我吗?它不起作用。提前谢谢!

1 个答案:

答案 0 :(得分:0)

您正在使用一种非常漫长而繁琐的方式来获取价值。使用复选框时,请尝试以下方法获取值。

var selected = new Array();
$('#checkboxes input:checked').each(function() {
    selected.push($(this).attr('name'));
});

并且可以使用此数组发送值。

但是当我看到您的HTML时,您根本不需要获取单个变量,因为所有复选框的名称都是“chk []”,它将数据作为数组发布。您可以直接提交表单,因为所有已检查的数组将可用:

$values = $_GET['chk'];
echo '<pre>';print_r($values);echo '</pre>'; 

它应该给出类型的结果(注意只有选中的复选框值才可用)。

array(1=>'greek',2=>'french') etc. 

然后,您可以使用foreach循环来管理值。