0

I am trying to take a json object stored in a textarea and convert it into a php array. I assign the value of the textarea to variable like $data = $_POST[‘data’] . When I submit the value of the text I use json_decode($data, true) to convert from JSON Object to php array. But nothing happens. It seems like nothing is assigned. How can I achieve the above?

EDIT: I have added quotes and made the suggestion below and is not working: DEMO

PHP

if(isset($_POST['submit'])) {
$data = $_POST['data'];
$personArray = json_decode($data, true);
print_r($personArray);
}

HTML

<textarea name="data">[{
    "firstName": "Jenny",
    "lastName": "LaRusso",
    "phone": "(555) 121-2121",
    "alt_phone": "(555) 123-4567",
    "main1": false,
    "main2": true    
}, {
    "firstName": "Sensei",
    "lastName": "Miyagi",
    "phone": "(555) 444-2222",
    "alt_phone": "(555) 999-1212",
    "main1": true,
    "main2": false
}]</textarea>
4
  • 1
    Just curious. Did you notice your textarea content before submit? Commented Mar 1, 2014 at 19:21
  • 1
    HTML looks nice, but have you inspected what $_POST['data'] contains? That's the data you're dealing with, regardless of where it came from. Commented Mar 1, 2014 at 19:22
  • Second that. Try printing out your input before it is parsed. var_dump($_POST['data']) should do. Commented Mar 1, 2014 at 19:23
  • side note, validation in most cases is very important, here is a great solution for validating the json in the texarea: stackoverflow.com/questions/6041741/… Commented Mar 1, 2014 at 19:44

3 Answers 3

3

I think in proper JSON, the keys (like firstName) need also be enclosed in quotes.

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

2 Comments

I have added the appropriate quotes but it is not working. Check DEMO
Are you sure? That JSON is valid now, according to jsonlint.com, so maybe the (next) problem is somewhere else.
2

Change your PHP code to

if(isset($_POST['data'])) {
$data = $_POST['data'];
$data = stripslashes($data); //Stripslashes removes all backslashes :)
$personArray = json_decode($data, true);
print_r($personArray);
}

Your JSON object should be this way inside the textarea

[{
"firstName": "Jenny",
"lastName": "LaRusso",
"phone": "(555) 121-2121",
"alt_phone": "(555) 123-4567",
"main1": false,
"main2": true    
}, {
"firstName": "Sensei",
"lastName": "Miyagi",
"phone": "(555) 444-2222",
"alt_phone": "(555) 999-1212",
"main1": true,
"main2": false
}]

Happy Coding :)

2 Comments

It is not working, check DEMO
OOPS, I forgot about the limitations of using "" in your text. I have edited the answer above :)
0

There does not seem to be anything wrong with your PHP code. For debugging, after setting $personArray, try adding these two lines:

var_dump($data);
var_dump($personArray);

This should lead to why you are having trouble.

Here you can see what each return type means(if $returnArray is equal to false): http://php.net/json_decode

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.