Ajax POST call doesn't send or submit any data












-1














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.



$('#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:.



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









share|improve this question
























  • Check the console and the request response for any errors. Check your PHP logs for any errors.
    – Script47
    Nov 12 at 12:32






  • 1




    if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") - Seems redundant as you are already checking $_POST, no need to check the REQUEST_METHOD too also, what is in searchDataConn.php?
    – Script47
    Nov 12 at 12:33








  • 3




    data: msg - Should be an object { textareaSubmitData: msg }. VTC as typo.
    – Script47
    Nov 12 at 12:34












  • @Script47 that's my connection set-up. I used it as an object, now the alert shows nothing. Want me to update the post so u can see what I have?
    – Dominus Providebit
    Nov 12 at 12:36












  • you are not passsing any data(object). All you are sending is a string
    – Akintunde-Rotimi
    Nov 12 at 12:37
















-1














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.



$('#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:.



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









share|improve this question
























  • Check the console and the request response for any errors. Check your PHP logs for any errors.
    – Script47
    Nov 12 at 12:32






  • 1




    if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") - Seems redundant as you are already checking $_POST, no need to check the REQUEST_METHOD too also, what is in searchDataConn.php?
    – Script47
    Nov 12 at 12:33








  • 3




    data: msg - Should be an object { textareaSubmitData: msg }. VTC as typo.
    – Script47
    Nov 12 at 12:34












  • @Script47 that's my connection set-up. I used it as an object, now the alert shows nothing. Want me to update the post so u can see what I have?
    – Dominus Providebit
    Nov 12 at 12:36












  • you are not passsing any data(object). All you are sending is a string
    – Akintunde-Rotimi
    Nov 12 at 12:37














-1












-1








-1







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.



$('#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:.



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









share|improve this question















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.



$('#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:.



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






javascript php jquery ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 12:50









Nik

1189




1189










asked Nov 12 at 12:30









Dominus Providebit

216




216












  • Check the console and the request response for any errors. Check your PHP logs for any errors.
    – Script47
    Nov 12 at 12:32






  • 1




    if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") - Seems redundant as you are already checking $_POST, no need to check the REQUEST_METHOD too also, what is in searchDataConn.php?
    – Script47
    Nov 12 at 12:33








  • 3




    data: msg - Should be an object { textareaSubmitData: msg }. VTC as typo.
    – Script47
    Nov 12 at 12:34












  • @Script47 that's my connection set-up. I used it as an object, now the alert shows nothing. Want me to update the post so u can see what I have?
    – Dominus Providebit
    Nov 12 at 12:36












  • you are not passsing any data(object). All you are sending is a string
    – Akintunde-Rotimi
    Nov 12 at 12:37


















  • Check the console and the request response for any errors. Check your PHP logs for any errors.
    – Script47
    Nov 12 at 12:32






  • 1




    if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") - Seems redundant as you are already checking $_POST, no need to check the REQUEST_METHOD too also, what is in searchDataConn.php?
    – Script47
    Nov 12 at 12:33








  • 3




    data: msg - Should be an object { textareaSubmitData: msg }. VTC as typo.
    – Script47
    Nov 12 at 12:34












  • @Script47 that's my connection set-up. I used it as an object, now the alert shows nothing. Want me to update the post so u can see what I have?
    – Dominus Providebit
    Nov 12 at 12:36












  • you are not passsing any data(object). All you are sending is a string
    – Akintunde-Rotimi
    Nov 12 at 12:37
















Check the console and the request response for any errors. Check your PHP logs for any errors.
– Script47
Nov 12 at 12:32




Check the console and the request response for any errors. Check your PHP logs for any errors.
– Script47
Nov 12 at 12:32




1




1




if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") - Seems redundant as you are already checking $_POST, no need to check the REQUEST_METHOD too also, what is in searchDataConn.php?
– Script47
Nov 12 at 12:33






if (isset($_POST['submit']) && $_SERVER['REQUEST_METHOD'] === "POST") - Seems redundant as you are already checking $_POST, no need to check the REQUEST_METHOD too also, what is in searchDataConn.php?
– Script47
Nov 12 at 12:33






3




3




data: msg - Should be an object { textareaSubmitData: msg }. VTC as typo.
– Script47
Nov 12 at 12:34






data: msg - Should be an object { textareaSubmitData: msg }. VTC as typo.
– Script47
Nov 12 at 12:34














@Script47 that's my connection set-up. I used it as an object, now the alert shows nothing. Want me to update the post so u can see what I have?
– Dominus Providebit
Nov 12 at 12:36






@Script47 that's my connection set-up. I used it as an object, now the alert shows nothing. Want me to update the post so u can see what I have?
– Dominus Providebit
Nov 12 at 12:36














you are not passsing any data(object). All you are sending is a string
– Akintunde-Rotimi
Nov 12 at 12:37




you are not passsing any data(object). All you are sending is a string
– Akintunde-Rotimi
Nov 12 at 12:37












3 Answers
3






active

oldest

votes


















1














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. So as you (now) just send 'textareaSubmitData' - check if that is set...



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





share|improve this answer





























    1














    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);
    }
    });
    });





    share|improve this answer





























      0














      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.



      On the host side, you can also do things like print_r($_POST) so that you can once again see what PHP has received.



      My experience is that, once you can see what's happening, debugging is very quick and easy. Whereas, guessing leads nowhere.






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262244%2fajax-post-call-doesnt-send-or-submit-any-data%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        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. So as you (now) just send 'textareaSubmitData' - check if that is set...



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





        share|improve this answer


























          1














          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. So as you (now) just send 'textareaSubmitData' - check if that is set...



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





          share|improve this answer
























            1












            1








            1






            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. So as you (now) just send 'textareaSubmitData' - check if that is set...



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





            share|improve this answer












            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. So as you (now) just send 'textareaSubmitData' - check if that is set...



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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 12:45









            Nigel Ren

            24.7k61832




            24.7k61832

























                1














                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);
                }
                });
                });





                share|improve this answer


























                  1














                  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);
                  }
                  });
                  });





                  share|improve this answer
























                    1












                    1








                    1






                    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);
                    }
                    });
                    });





                    share|improve this answer












                    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);
                    }
                    });
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 at 12:46









                    Kamal Paliwal

                    730310




                    730310























                        0














                        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.



                        On the host side, you can also do things like print_r($_POST) so that you can once again see what PHP has received.



                        My experience is that, once you can see what's happening, debugging is very quick and easy. Whereas, guessing leads nowhere.






                        share|improve this answer


























                          0














                          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.



                          On the host side, you can also do things like print_r($_POST) so that you can once again see what PHP has received.



                          My experience is that, once you can see what's happening, debugging is very quick and easy. Whereas, guessing leads nowhere.






                          share|improve this answer
























                            0












                            0








                            0






                            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.



                            On the host side, you can also do things like print_r($_POST) so that you can once again see what PHP has received.



                            My experience is that, once you can see what's happening, debugging is very quick and easy. Whereas, guessing leads nowhere.






                            share|improve this answer












                            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.



                            On the host side, you can also do things like print_r($_POST) so that you can once again see what PHP has received.



                            My experience is that, once you can see what's happening, debugging is very quick and easy. Whereas, guessing leads nowhere.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 14:33









                            Mike Robinson

                            4,00421021




                            4,00421021






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262244%2fajax-post-call-doesnt-send-or-submit-any-data%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Bressuire

                                Vorschmack

                                Quarantine