0

Hi,
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.

Cheers,
Vishal

2
Contributors
2
Replies
6
Views
6 Years
Discussion Span
Last Post by vishalkhialani
0

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.

0

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

Cheers,
Vishal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.