简体   繁体   中英

jQuery $.post not passing all data

I've got a jQuery $.post script which isn't passing all of the data to the php script it's calling. It worked fine when I only passed two parameters into the data field, but now that I've got more than just two, it's not working any more. The console is showing the values of the fields, but the data isn't being inserted into the database for some reason.

HTML Form

<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 Script

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 Script

$("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");
})

In your php script, the single quotes are the wrong type of single quotes.

Change:

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

To:

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

First, confirm that you aren't getting the proper values back by writing out the values of the three post variables. Be sure that its a problem with the jquery post.

If it is a jquery problem, which parameter isn't getting passed back?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM