0

I Have a simple array which looks like

var arr = ["foo", "2015/11/04", "Jill", "Smith", "60"]
var serializedArr = JSON.stringify( arr );

now can you please let me know how I can load the serializedArr into the data in following ajax request?

var upload = $.ajax({
    type: "POST",
    url: "assets/.../loader.php",
    data: data,
    cache: false,
    beforeSend: function() {
    }
});

and also how to read the data on server side PHP file? Thanks

1

1 Answer 1

1
var upload = $.ajax({
    type: "POST",
    url: "assets/.../loader.php",
    data: {array:serializedArr},// pass here{key:value,key:value,...}
    cache: false,
    beforeSend: function() {
    }
});

In php

$array = json_decode($_POST['array']);
print_r($array);// this will get back the array which was encoded.

You have to decode the array because, array is encoded(JSON.stringify(arr))

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Niranjan, just on PHP side can you please let me know incase of having a variable like $foo how can I get it's value from the $array I mean the first item in the arr
use foreach() on $array, and get the values .
why foreach()? I just need the first element? in a only one set of array
echo $array[0];. But this is simply spoon feeding nothing else
if you want only 1 element, then dont encode r aont use array. just send as normal variable.

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.