使用 AJAX 将复选框值 1(如果未选中)传递给 PHP(如果未选中)

Pass checkbox values 1 if checked if not 0to PHP using AJAX

我想使用 php 复选框将“0”或“1”插入 mysql 数据库,但无法将值存储到数据库中! 我正在尝试插入这样的复选框数组:

1: 已选择

0:未选中

[answer] => Array
        (
            [0] => answer 1
            [1] => answer  2
            [2] => answer  3
        )

[check] => Array
        (
            [0] => 1
            [1] => 0
            [2] => 1
        )

php 文件:

<td> <!-- <input type="hidden"  name="check[]" value="0" >-->
     <input type="checkbox" id="check" name="check[]" value="1">
</td>

在通过 AJAX 的过程文件中,我存储了答案数据库,但我无法为每个选定的答案存储选中的值

$query = mysql_query("INSERT INTO questions 
                         (nom_question,id_categorie)       
                     VALUES('$question','$categorie')", $conn) 
                     or die(mysql_error());

//last insert id question
$last_id = mysql_insert_id();

$check =  $_POST['check']; 

foreach ($reponse as $key=>$value  ) {
    $values = mysql_real_escape_string($value);
    $x='1';
    $query = mysql_query("INSERT INTO reponses 
                         (nom_reponse,id_question,id_categorie,correct) 
                         VALUES ('$values',
                                 '$last_id',
                                 '$categorie',
                                 '$x')") 
                or die(mysql_error());

请帮忙!如果检查或不检查,我如何存储答案谢谢。

用 ajax
为 post 试试这个 将 id 更改为 class 以方便选择

var selectedCB = new Array();
$('.cbcheck:checked').each(function() {
            selectedCB.push(this.value);
          });
          $.ajax({
            url: 'insert.php',
            dataType: 'text',
            type: 'post',
            contentType: 'application/x-www-form-urlencoded',
            data: {recordsval: selectedCB},
            success: function( data){
              $('#result').html(data);
            },
            error: function(errorThrown ){
              console.log( errorThrown );
            }
          });


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" class="cbcheck" name="check[]" value="1">
<input type="checkbox" class="cbcheck" name="check[]" value="0">
<input type="checkbox" class="cbcheck" name="check[]" value="1">
<input type="submit" value="submit">
<div id="result"></div>

和php句柄

<?php
$data = $_POST['recordsval'];
var_dump($data);
?>

用您的代码对此进行调整...

非常感谢先生的重播;但问题是复选框是动态的,所选值取决于用户! 我使用 serliaze 表单通过 ajax 发送锅数据:复选框的形式为

<form name="form" id="form" role="form" method="post" action="">
<<tr>
                        <td>Answer</td>
                        <td><input type="text" id="name" name="answer[]"></td>

                        <td>

                                <input type="checkbox" id="check" name="check[]" ></td>
                        <td><input type="button" value="Ajouter" " class="button"></td>
                    </tr>
</form>

我怎么才能选出选中的答案呢!如果答案为真,如果不将 0 存储在数据库 'correct' 中的列

中,这个想法就是存储一个

我尝试通过这种方式处理数据 AJAX

 $(function() {
                $("#form").on("submit", function(event) {
                    event.preventDefault();

                    $.ajax({
                        url: "test.php",
                        type: "post",
                        data: $(this).serialize(),
                        success: function(d) {
                            alert(d);
                        }
                    });
                });
            });
            });

它真的很简单,你必须记住 CheckBoxes 只会从 html 表单发送到脚本,IF 它们实际上是被选中的,所以未选中复选框只是不存在于 $_POST 或 $_GET 数组中。

这并没有看起来那么大的问题,您需要做的就是检查复选框是否存在,如果存在则进行检查。

所以在你的代码中而不是这样做

$check =  $_POST['check']; 

这样做

$check =  isset($_POST['check']) ? 1 : 0; 

我会更进一步并展示将其存储在数据库中的代码,但您的代码实际上并没有在任何地方使用 $check 变量。