0

I am trying to pass a simple jquery array with ajax, and when I use a var_dump in php it shows null, but when I show the output through jquery the array appears.

Jquery

aux = ['ahsja', 'shjdshjd', 'dh8f8sfs8'];

        var json = JSON.stringify(aux);
        $.ajax({
            type: "POST",
            url: "test.php",
            dataType: 'json',
            data: {date: json},

            success: function (res) {
                window.open(this.url, '_blank');
                console.log(JSON.stringify(res));

            },
            error: function (res) {
                console.log(JSON.stringify(res));
                window.open(this.url, '_blank');
            }
        });

PHP

$date= json_decode($_POST['date']);

var_dump($date);
  • whats it return before json_decode ? –  Aug 03 '17 at 20:52
  • 1
    When you say it shows null, how are you trying to **view** this PHP output? I take it that it's on a **different** page to your jQuery? An AJAX POST happens in the background; once you navigate manually to the PHP page yourself in order to see the output, the `$_POST` value will no longer exist, as you'll be accessing the PHP page via `$_GET`. – Obsidian Age Aug 03 '17 at 20:53
  • Actually:-`data: {date: json},`Need to be:-`data: {date: aux},`Note:- variable `json` exist no-where in your code – Alive to die - Anant Aug 03 '17 at 21:00
  • Despite this question being closed (meaning I can no longer answer) for you not serializing your object, it's important to note that your `POST` **is** going through. You simply can't see the output because you need to **use** `$date` on the PHP page, then **echo** something out. The echo'ed content can be returned in the AJAX's `res`. – Obsidian Age Aug 03 '17 at 21:14
  • @AlivetoDie `var json = JSON.stringify(aux);` –  Aug 03 '17 at 21:35

0 Answers0