0

I am trying to send a simple json object by $_POST to a php from an AJAX call. But everything I do is returning an empty array. Past posts have suggested using 'file_get_contents(x)', 'if(isset(x)){}' and nothing seems to work.

  print_r($_POST);    
  $data = file_get_contents('php://input');
  echo "line before";
  print_r(json_decode($data, true));
  //return json_decode($data);
  if (isset($_POST['x']))
  {  
    $myfile = $_POST['x'];
    print_r($myfile);
    print"hello paul, here I am";
  } else {
    print"something is rotten in the country of Denmark";
  }

I have attempted to read and access the data multiple ways. the first print_r returns an empty array, the second print_r does not even fire, and the if fails.

On the Javascript side I have tried multiple ways of sending the data both with and without JSON_stringify(). I have tried sending a string instead of JSON, as well as sending JSON data in the data stream.

$.ajax({
        type: 'POST',
        url: 'admo.php/',
        data: x,
        //datatype: "JSON",
        success: function() {
            console.log("Success: " + x)
            window.open("admo.php");
        },
        error: function(x) {
            console.log("Error: " + x)
            console.log("you errored out....some where");
        }
    });

The data x could be any json file, just for an example

{"AlarmClockTimes": ["10:24", "5:13", "14:43", "17:00"],
"RadioStatus": "off",
"RadioStations": ["wamu", "bluegress", "soil", "comedy"]} 

I get the window to open, and the console.log to print which tells me that is it is sending, but PHP is not receiving. Any help would be greatly appreciated.

Per a suggestion about network and HTTP response codes, they both come back good. When using the network tab on chrome developer, I see a HTTP response of 200 and see the form data is correct.

Paul Carlson
  • 379
  • 5
  • 18
  • Is it running but not getting the POST data, or is it simply not running the program at all? What does the http response look like? Is there a response code? Is it 200? – Spudley Oct 10 '18 at 10:59
  • 1
    Have you had a look in the client for example developer tools in Chrome? There you can see what is happening and what is being sent from the client. The request payload should be visible under the "network" tab, https://stackoverflow.com/questions/15603561/how-can-i-debug-a-http-post-in-chrome – KungWaz Oct 10 '18 at 11:01
  • I'm not sure what you mean: `the second print_r does not even fire, and the if fails.`. How can the `if` fail if the second `print_r` does not even fire? – jeroen Oct 10 '18 at 11:02
  • 1
    @Paul - Are you trying to access the passing values in the page where you open as a new tab / window? – Anjana Silva Oct 10 '18 at 12:57
  • @Spudley It is working, but I am not getting POST data. I have not been looking for an http response, as I was using a success in ajax call. – Paul Carlson Oct 10 '18 at 22:47
  • @KungWaz I have not looked at the network tab, but will look at the link and look into that. – Paul Carlson Oct 10 '18 at 22:51
  • @jeroen the second print_r (print_r(json_decode($data, true));) does not print anything. Then the if statement goes to the else response. – Paul Carlson Oct 10 '18 at 22:52
  • @AnjanaSilva I do neccearly need access to the values, nor the page I am doing this to make sure that it is actually working. – Paul Carlson Oct 10 '18 at 22:52
  • Thanks for all the suggestions, I finally got it working. In regards to AnjanaSilva I was trying to open the page on success. However, when doing that I actually would not have sent any information over. Once I took out the "window.open(admo.php)" it worked as I expected. – Paul Carlson Oct 11 '18 at 02:17

0 Answers0