php - 使用 $.post() 的 JQuery POST 表单

标签 php jquery html forms web

我有以下表格

<form id="colorTest" method="POST">
    <input type="text" id='responseText' name="responseText" value="">
    <input type="button" id='confirm' name="confirm" value="confirm">
    <input type='text' id="idNum" name="idNum">
    <input type='text' id='correct' id="correct">
</form>

实际上只输入了#responseText 的值,#idNum 是从另一个页面传递过来的,而其他字段由这个函数填充:

<script type="text/javascript">
    var arr = [2,5,6,7,10,16,29,42,57];
    x = 0
    /* idNum is passed from another page to this one */
    $(document).ready(function() {
        document.getElementById('idNum').value = localStorage.memberID;
        /* when something is typed in the form */
        $('#responseText').on('input', function() {
            /* the value is stored */
            var response = document.getElementById('responseText').value;
            /* and compared with the vector arr that contains the correct answers*/
            if( response == arr[x]){
                $("#correct").attr('value', 'yes');
            }else{ 
                $("#correct").attr('value', 'no');
            };
        });
       /* confirm is the button to send the asnwer */
       $('#confirm').click(function(){
           $.post("testColor.php", $("#colorTest").serialize());
           x = x + 1;
       });
   });      
</script>

这是我的 php:

<?php
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $response = $_POST["responseText"];
    $memberID = $_POST["idNum"];
    $timeStmp = time();
    $correct = $_POST["correct"];

    $sql = "INSERT INTO colorBlind (memberID, response, correct, timeStmp)
            VALUES ('$memberID', '$response' ,'".$correct."','".$timeStmp."')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn -> close();
?>

现在,我确定在#confirm 上发生点击事件时正确调用了 php,因为在 php 中调用的时间戳记录在我的数据库中。尽管如此,字段中的值都没有传递到 php.ini 文件中。即如果做类似的事情:

echo $_POST["idNum"];

我得到一个空字段。

有人知道我做错了什么或有任何关于如何调试它的建议吗?我已经在这上面花了几天时间,但没有任何进展。

最佳答案

我注意到的第一件事是你的

<input type='text' id='correct' id="correct"> 

包含 id="correct" 两次。我确定您打算添加 name="correct"

当您使用 $("#colorTest").serialize() 时,将返回

responseText=foo&idNum=bar&correct=foobar

我可以看到如何将其用于 GET 请求。但是当您使用 $.post() 时,您会希望为您的 POST 数据传递一个键/值对对象。

例如,这应该有效:

var params = {
    responseText: 'foo',
    idNum: 'bar',
    correct: 'foobar'
};
$.post("testColor.php", params);

关于php - 使用 $.post() 的 JQuery POST 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446334/

相关文章:

javascript - 如何在鼠标悬停在另一个标签上时切换 div 的可见性

php - Google Plus API - 无法在特定日期之前处理 token

php - 导出表时如何创建新列。 Laravel maat网站/excel

php - 使用 PHP 从 MySQL 列中删除重复的逗号分隔值

php - 去年这个日期的 MySQL 语句

jquery动画与手机

javascript - 使用 JavaScript 在另一个字符串的多个位置插入字符串

javascript - Kineticjs:如何使用kineticjs使用钢笔工具?

javascript - 文本区域颜色值

jquery - 悬停时从底部到顶部动画移动 div