简体   繁体   English

Ajax POST调用不发送或提交任何数据

[英]Ajax POST call doesn't send or submit any data

I am trying to make my submit button send data to my PHP file without reloading, however, when I use this call it doesn't send any data and neither is saved in my database. 我试图使我的提交按钮无需重新加载即可将数据发送到我的PHP文件,但是,当我使用此调用时,它不发送任何数据,并且都不保存在数据库中。

$('#formSubmitData').on('submit', function(event) {
    event.preventDefault();
    var msg = $('#textareaSubmitData').val();
    $.ajax({
        url: 'searchData.php', //this is ALSO how the text is being send to the database to be retrieved later on.
        type: 'POST',
        data: {message:msg},
        success: function(data) {
            console.log(data);
            data = msg;
            alert(data);
        }
    });
});

The alert shows the correct value, but in my database, the rows remain empty. 警报显示正确的值,但是在我的数据库中,行保持为空。

How the PHP code looks like:. PHP代码如何:

    if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") {
        include_once 'dbConn.php';

        $name = $_SESSION['userName'];
        $msg = $_POST['textareaSubmitData'];        

        $stmt = $conn->prepare("INSERT INTO messages (name, message) VALUES (?, ?)");
        $stmt->bind_param('ss', $name, $msg);

        $name = $_SESSION['userName'];
        $msg = $_POST['textareaSubmitData'];
        $stmt->execute();   

        $conn->close();
        $stmt->close();

    } else {
        header('Location: index.php?send=failure');
        exit();         
    }
}

Think there are 2 issues, the first is that you need to make sure the data to send is an object and not just a value... 认为有两个问题,首先是您需要确保要发送的数据是对象,而不仅仅是值...

data: { textareaSubmitData: msg },

The second is that when you try and process the data, your first line is... 第二个是当您尝试处理数据时,第一行是...

if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") {

So this is looking for some POST data in 'submit' - which you don't send. 因此,这是在“提交”中寻找一些POST数据-您不会发送。 So as you (now) just send 'textareaSubmitData' - check if that is set... 因此,当您(现在)仅发送“ textareaSubmitData”时-检查是否已设置...

if (isset($_POST['textareaSubmitData']) && $_SERVER['REQUEST_METHOD'] === "POST") {

You are sending the value of submit button in data. 您正在发送数据中的提交按钮的值。 You need to send the form data to your server. 您需要将表单数据发送到服务器。

$('#formSubmitData').on('submit', function(event) {
    event.preventDefault();
    var data = new FormData(this);
    $.ajax({
        url: 'searchData.php', //this is ALSO how the text is being send to the database to be retrieved later on.
        type: 'POST',
        data: data,
        success: function(data) {
            data = msg;
            alert(data);
        }
    });
});

Also – definitively, "look at(!)" what is being sent, using the debugging features of your browser. 同样,使用浏览器的调试功能,明确地“查看(!)”正在发送的内容。 When the AJAX call goes off, you can see an HTML POST being done – so, you can see exactly what the URL is, and exactly what data is (or, isn't) being supplied. 当AJAX调用结束时,您可以看到 HTML POST已完成–因此,您可以确切地看到URL是什么,以及正在提供(或不提供)什么数据。

On the host side, you can also do things like print_r($_POST) so that you can once again see what PHP has received. 在主机方面,您还可以执行诸如print_r($_POST)以便再次查看 PHP收到了什么。

My experience is that, once you can see what's happening, debugging is very quick and easy. 我的经验是,一旦您看到发生了什么,调试就非常快速和容易。 Whereas, guessing leads nowhere. 猜测无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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