0

I want to sent a JS array to remote php server and convert it to a php array.
To check if php array is equal to js array I'm trying to get its members inside result div, but without success.

var rlist = ["title1", "title2", "title3"];
var b = JSON.stringify(rlist);
$.ajax({
        url: 'reorder.php',
        type: 'post',
        data: {'b': b},
        success: function(data) {
            $("#result").html(data);
        }
        });

reorder.php

$rlist = json_decode( $_POST['b'], true );
echo $rlist;
2
  • 3
    That should work, except that you cannot echo an array like that. Try a var_dump() to see the actual contents. Commented Jul 4, 2016 at 10:30
  • 2
    use echo "<pre>"; print_r(json_decode($_POST['b'], true)); in reorder.php. Commented Jul 4, 2016 at 10:31

2 Answers 2

1

try to put this code in the reorder.php

$rlist = json_decode( $_POST['b'], true );
foreach ($rlist as $value) {
    echo "<div>" . $value . "</div>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use json_decode as jQuery ajax submits it's arrays as json.

// Use as object
$json = json_decode($_POST['postdata']);

// Use as
echo $json->seomthign

$json = $_POST['postdata'];

// Output array
echo "<pre>";
var_dump(json_decode($json));
echo "</pre>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.