Below is the data format which I am passing to php via javascript in json format.

{"name":"Secret","value": "checked","name":"OldSpice","value": "unchecked","name":"Degree","value": "unchecked","name":"Dove","value": "unchecked","name":"Axe","value": "unchecked","name":"Maxim","value": "unchecked","name":"Nivea","value": "unchecked",}

I can access the data successfully using the code

echo $_POST['name'];

I want to convert it to an array. I tried to use json_decode() and below is my code but I am getting the below error and I don't know what to do.

$obj = json_decode($_POST);
echo $obj;
Warning: json_decode() expects parameter 1 to be string, array given

Any help will be highly appreciated.


Recommended Answers

All 2 Replies

You don't want to decode the entirety of $_POST. $_POST is, at it says, an array. You want to decode $_POST.

Also, you'll probably want to do a check if magic quotes is on, and strip any slashes if it is.

Finally, I see your JSON has a trailing comma at the end of the contents. If possible, get rid of that trailing comma - IE doesn't like them.

Hi quasipickle,
Appreciate the points mentioned. The comma one was something I did not notice.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.