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>
Code_Ed_Student
  • 1,180
  • 6
  • 27
  • 67
  • 1
    Just curious. Did you notice your textarea content before submit? – Your Common Sense Mar 01 '14 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. – GolezTrol Mar 01 '14 at 19:22
  • Second that. Try printing out your input before it is parsed. `var_dump($_POST['data'])` should do. – Aurel Bílý Mar 01 '14 at 19:23
  • side note, validation in most cases is very important, here is a great solution for validating the json in the texarea: http://stackoverflow.com/questions/6041741/fastest-way-to-check-if-a-string-is-json-in-php/6041773#6041773 – m79lkm Mar 01 '14 at 19:44

3 Answers3

3

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

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • I have added the appropriate quotes but it is not working. Check [DEMO](http://holaweblearning.co.nf/test/insert_person.php) – Code_Ed_Student Mar 01 '14 at 19:36
  • Are you sure? That JSON is valid now, according to http://jsonlint.com/, so maybe the (next) problem is somewhere else. – GolezTrol Mar 01 '14 at 19:56
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 :)

prateekkathal
  • 3,482
  • 1
  • 20
  • 40
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

Akshay Kalose
  • 787
  • 1
  • 5
  • 15