0

I want to send the key and value pairs to a PHP file using jQuery's AJAX function, however the function is not sending the data.

The PHP code is in the same "Tester.php" file together with the HTML code as shown below:

<?php
if (array_key_exists("REQUEST_METHOD", $_SERVER) && $_SERVER["REQUEST_METHOD"] == "POST") {
    echo "<pre>";
    print_r($_POST); // always empty, no clue why!
    echo "</pre>";
    exit();
}
?>

<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $.ajax({
                type: "POST",
                url: "Tester.php", // the same file/page
                data: {
                    requestData: true,
                    message: "please print me!"
                },
                success: function(data) {
                    document.write("success!");
                    document.write(data);
                },
                error: function(xmlHttp) {
                    document.write("error!");
                    document.write(xmlHttp.responseText);
                }
            });
        });
    </script>
</head>
<body>
    <p>Testing...</p>
</body>
</html>

This prints:

success!
Array
(
)

But the array printed should contain the "requestData: true" from the data passed to the $_POST array, but instead this array is empty. What have I done wrong? Thank you!

Mayron
  • 2,146
  • 4
  • 25
  • 51
  • your html file and php code file are on same page or different ? – Hirendrasinh S. Rathod Apr 09 '16 at 12:01
  • They're on the same page. It's all in one file – Mayron Apr 09 '16 at 12:02
  • separate both of files and make sure you had place comma in data as @Rmidi Ayoub suggested. – Hirendrasinh S. Rathod Apr 09 '16 at 12:04
  • @HirendrasinhS.Rathod The comma is not the problem as I just added that message line on stackoverflow to make the problem easier to identify, but I've fixed that now. Also the original problem was on 2 separate files and still did not work. I put all of the code inside 1 file just because I thought it would be easier to explain. – Mayron Apr 09 '16 at 12:06
  • @Mayron review my working example. – Hirendrasinh S. Rathod Apr 09 '16 at 12:17
  • @Mayron if still you facing issue then install firebug and check in console that acctually request has been executed and what data will send to request file. – Hirendrasinh S. Rathod Apr 09 '16 at 12:18
  • What if you remove `document.write("success!");` on success callback? Also I don't think you need to ask PHP whether `REQUEST_METHOD" is set or no. – Chay22 Apr 09 '16 at 12:20
  • @Chay22 I only asked whether it was set because it originally was in the same file. But if you split it into 2 then it's not necessary. – Mayron Apr 09 '16 at 12:23
  • @HirendrasinhS.Rathod I have firebug installed but it reports no errors. I'm wondering if it's a bug with PHP or something else. – Mayron Apr 09 '16 at 12:24
  • @Mayron I didn't ask you to separate it., just suggested it's not necessary to do `array_key_exists("REQUEST_METHOD", $_SERVER)`. And I guessed it doesn't work because `write()` overwrites everything on a page load. Dunno... – Chay22 Apr 09 '16 at 12:32

1 Answers1

1

Html File (36516400.html)

<html>
    <head>
        <title>36516400</title>
        <script type="text/javascript" src="../../../assets/js/script.js"></script>
    </head>

    <body>
        <h1>Welcome</h1>
        <script type="text/javascript">
            $(document).ready(function(){
                $.ajax({
                    type:'POST',
                    data:{
                        'requestData':true,
                        'message':"please print me!"
                    },
                    url:'responce.php',
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    success:function(data){
                        alert(data);
                    }
                });
            })
        </script>
    </body>
</html>

PHP file (responce.php)

<?php
    echo "<pre>";
    print_r($_REQUEST);
    echo "<pre>";
?>

Request in chrome console

enter image description here

  • Weird, for me that also alerts an empty array. – Mayron Apr 09 '16 at 12:22
  • 1
    please review image i had added, you got this formData block – Hirendrasinh S. Rathod Apr 09 '16 at 12:28
  • Ah yes I am getting that. But it should be inside the $_POST array. Maybe I need to set the content type if it thinks it's a form. – Mayron Apr 09 '16 at 12:31
  • http://stackoverflow.com/questions/14322984/differences-between-contenttype-and-datatype-in-jquery-ajax-function for more details on content type. – Hirendrasinh S. Rathod Apr 09 '16 at 12:37
  • Cannot figure out why $_REQUEST or $_POST is empty but I can see this formData block. How do I print that so it can be returned to the ajax's success function? – Mayron Apr 09 '16 at 12:37
  • should you try method:'POST' instead of type:'POST' once ? – Hirendrasinh S. Rathod Apr 09 '16 at 12:40
  • please add (headers: {'Content-Type': 'application/x-www-form-urlencoded'}) additionally. – Hirendrasinh S. Rathod Apr 09 '16 at 12:50
  • I assume it's some weird bug where the code is fine but it's still failing. Tried both of those with no luck. It's definitely type: "POST". – Mayron Apr 09 '16 at 12:58
  • I think it's a bug with my IDE phpStorm that I'm using. Will look into it :) Thanks for your help though. – Mayron Apr 09 '16 at 13:02
  • i am also using phpStrom. had you try to encode your .php or .html with utf-8 or utf-8 without bom using notpade++. any way if you find solution inform us so if we got this kind of issue we can kick out it. – Hirendrasinh S. Rathod Apr 09 '16 at 16:47
  • It was just a matter of using the phpStorm debugger to show the webpage on localhost:6503 rather than using Apache's localhost. By switching to using Apache instead, it fixed the strange POST data issue. – Mayron Apr 09 '16 at 22:01
  • Sorry for my bad wording, not sure how else to describe it in a more technically accurate way :) – Mayron Apr 09 '16 at 22:02