简体   繁体   中英

Ajax send data using POST method, but PHP function does not INSERT them to the table

I have a problem with receiving data from AJAX using the POST method.

At first, everything worked properly, but then something happened and now I can't find the bug.
If I check my AJAX everything dispatched properly, but PHP does not receive the data. As a result, it does not insert data into the database table.

My jQuery:

$('#button-send-review').click(function(e) {
    e.preventDefault();
    var th = $(this);
    var name = $("#name_review").val();
    var good = $("#good_review").val();
    var bad = $("#bad_review").val();
    var comment = $("#comment_review").val();
    var iid = $("#button-send-review").attr("iid");
    var add_review = $("#button-send-review").val();

    if (name != "") {
        var name_review = '1';
        $("#name_review").css("borderColor", "#DBDBDB");
    } else {
        var name_review = '0';
        $("#name_review").css("border", "2px solid #d20000");
    }

    if (good != "") {
        var good_review = '1';
        $("#good_review").css("borderColor", "#DBDBDB");
    } else {
        var good_review = '0';
        $("#good_review").css("border", "2px solid #d20000");
    }

    if (bad != "") {
        var bad_review = '1';
        $("#bad_review").css("borderColor", "#DBDBDB");
    } else {
        var bad_review = '0';
        $("#bad_review").css("border", "2px solid #d20000");
    }

    if (name_review == '1' && good_review == '1' && bad_review == '1') {
        $.ajax({
            type: "POST",
            url: "./",
            data: "goods_id=" + iid + "&name=" + name + "&good=" + good + "&bad=" + bad + "&comment=" + comment + "&add_review=" + add_review,
            dataType: "html",
            cache: false,
        }).done(function() {
            $(".success").addClass("visible");
            setTimeout(function() {
                // Done Functions
                th.trigger("reset");
                $(".success").removeClass("visible");
                $.magnificPopup.close();
            }, 3000);
        });
    }
});

My Controller:

<?php
if ($_POST['add_review']) {
    add_review();
}
?>

My Model:

<?php
function add_review() {
    global $link;

    $goods_id = trim($_POST['goods_id']);
    $name = trim($_POST['name']);
    $good = trim($_POST['good']);
    $bad = trim($_POST['bad']);
    $comment = trim($_POST['comment']);

    $goods_id = clear($goods_id);
    $name = clear($name);
    $good = clear($good);
    $bad = clear($bad);
    $comment = clear($comment);

    $query = "INSERT INTO reviews(goods_id, name, good_reviews, bad_reviews, comment, date)
              VALUES($goods_id, '$name', '$good', '$bad', '$comment', NOW())";
    $res = mysqli_query($link, $query) or trigger_error($link->error . "[DB]");

    return true;
}
?>

My HTML:

<div id="send-review" class="popup-form">
    <div class="success">Thank you! <br>
        Your review send for moderation.
    </div>
    <h4 id="title-review">The review would be posted soon.</h4>
    <ul>
        <li>
            <label id="label-name"><span>Name *</span></label>
            <input maxlength="15" type="text" id="name_review" placeholder="Enter your name..." style="border-color: rgb(219, 219, 219);" />
        </li>
        <li>
            <label id="label-good"><span>Pros *</span></label>
            <textarea id="good_review" placeholder="Enter pros..." style="border-color: rgb(219, 219, 219);"></textarea>
        </li>
        <li>
            <label id="label-bad"><span>Cons *</span></label>
            <textarea id="bad_review" placeholder="Enter cons..." style="border-color: rgb(219, 219, 219);"></textarea>
        </li>
        <li>
            <label id="label-comment">Comment</label>
            <textarea id="comment_review" placeholder="Your comment..."></textarea>
        </li>
    </ul>
    <div class="button-wrap">
        <button class="button" type="submit" id="button-send-review" name="add_review" value="Send" iid="92">Send</button>
    </div>
    <button title="Close (Esc)" type="button" class="mfp-close">&times;</button>
</div>

Your $.ajax() data isn't formatted correctly;

Should be like this example to receive the POST strings in your receiver page:

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
});

And please use prepared SQL statements to secure against SQL Injections.

The posting format in the ajax method is incorrect;

change ajax code to :

<script>
$('#button-send-review').click(function (e) {
    e.preventDefault();
    var th = $(this);
    var name = $("#name_review").val();
    var good = $("#good_review").val();
    var bad = $("#bad_review").val();
    var comment = $("#comment_review").val();
    var iid = $("#button-send-review").attr("iid");
    var add_review = $("#button-send-review").val();

    if (name != "")
    {
        var name_review = '1';
        $("#name_review").css("borderColor", "#DBDBDB");
    } else {
        var name_review = '0';
        $("#name_review").css("border", "2px solid #d20000");
    }

    if (good != "")
    {
        var good_review = '1';
        $("#good_review").css("borderColor", "#DBDBDB");
    } else {
        var good_review = '0';
        $("#good_review").css("border", "2px solid #d20000");
    }

    if (bad != "")
    {
        var bad_review = '1';
        $("#bad_review").css("borderColor", "#DBDBDB");
    } else {
        var bad_review = '0';
        $("#bad_review").css("border", "2px solid #d20000");
    }



    if (name_review == '1' && good_review == '1' && bad_review == '1')
    {
        $.ajax({
            type: "POST",
            url: "./",
            data: {"goods_id":iid,"name":name,"good":good,"bad":bad, "comment":comment,"add_review":add_review},
            dataType: "html",
            cache: false,
        }).done(function () {
            $(".success").addClass("visible");
            setTimeout(function () {
                // Done Functions
                th.trigger("reset");
                $(".success").removeClass("visible");
                $.magnificPopup.close();
            }, 3000);
        });
    }
});
</script>

use PDO to stop SQl Injection

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