jQuery $ .post没有传递所有数据

时间:2014-10-26 00:35:11

标签: javascript php jquery html

我有一个jQuery $ .post脚本,它没有将所有数据传递给它调用的php脚本。当我只将两个参数传递到数据字段时,它工作得很好,但是现在我只有两个以上的参数,它不再工作了。控制台显示字段的值,但由于某种原因,数据未插入数据库。

HTML表单

<fieldset for="center">
    <label>Center:</label>
    <div class="select" name="center" id="center">
        <div class="arrow"></div>
        <div class="option-menu">
            <?php
                $query = "SELECT * FROM $centers";
                $result = mysqli_query($connect, $query);
                $center_name;
                while($row = mysqli_fetch_assoc($result)){
                    $center_name = "{$row['center']}";
                    echo "<div class='option'>" .$center_name ."</div>";
                }
            ?>
        </div>
    </div>
</fieldset>
<fieldset for="initials">
    <label>Initials:</label>
    <input type="text" name="initials" id="initials" />
</fieldset>
<fieldset for="recurrent">
    <label>Type:</label>
    <div class="select" name="recurrent" id="recurrent">
        <div class="arrow"></div>
        <div class="option-menu">
            <?php
                $query = "SELECT * FROM $recurrent";
                $result = mysqli_query($connect, $query);
                while($row = mysqli_fetch_assoc($result)){
                    $type = "{$row['type']}";
                    echo "<div class='option'>" .$type ."</div>";
                }
            ?>
        </div>
    </div>
</fieldset>

PHP脚本

ob_start();
require("../includes/header.php");
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $center = $_POST["center"];
    $recurrent = $_POST["recurrent"];
    $initials = $_POST["initials"];
    $query = "INSERT INTO `$scenarios`(`initials`, `center`, `recurrent`) VALUES('" .$initials ."', " .$center ."', '" .$recurrent ."')";

    mysqli_query($connect, $query);
}

    ob_clean();
    echo json_encode(array("success" => 1));

jQuery脚本

$("input[id='save']").on("click", function(){
    var initials = $("#initials").val();
    var center = $("#center_menu").val();
    var recurrent = $("#recurrent_menu").val();
    console.log(initials);
    console.log(center);
    console.log(recurrent);
    $.post("../php/processing.php", {initials: initials, center: center, recurrent: recurrent}, function(response){
        if(response.success == "1"){
        }
    }, "json");
})

2 个答案:

答案 0 :(得分:0)

在您的php脚本中,单引号是错误的单引号类型。

更改:

$query = "INSERT INTO `$scenarios`(`initials`, `center`, `recurrent`) VALUES('" .$initials ."', " .$center ."', '" .$recurrent ."')";

$query = "INSERT INTO '$scenarios'('initials', 'center`, 'recurrent') VALUES('" .$initials ."', " .$center ."', '" .$recurrent ."')";

答案 1 :(得分:0)

首先,通过写出三个post变量的值来确认您没有得到正确的值。确保它与jquery帖子有问题。

如果是jquery问题,哪个参数没有被传回?