0

I'm just starting out in Angular and I'm trying to update my JSON file with a PHP script. The script updates it with the right keys but null values. I'd really appreciate some help.

Here's my code:

<?php
$object_data = array(
'capital' => $_POST['in1'],
'country' => $_POST['in2'],
'continent' => $_POST['in3'],
'image' => $_POST['in4']
);
$capitals = file_get_contents('capitals.json');
$capital_data = json_decode($capitals);

array_push($capital_data,$object_data);
$jsondata = json_encode($capital_data,JSON_PRETTY_PRINT);

file_put_contents('capitals.json',$jsondata);
?>

And here's my JSON file:

[
{
    "capital": "Ottawa",
    "country": "Canada",
    "continent": "North America",
    "image": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/2\/22\/Parliament-Ottawa.jpg"
},
{
    "capital": "Oslo",
    "country": "Norway",
    "continent": "Europe",
    "image": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/b\/bd\/Downtown_Oslo_Norway_skyline.png\/800px-Downtown_Oslo_Norway_skyline.png"
},
{
    "capital": "Auckland",
    "country": "New Zealand",
    "continent": "Oceania",
    "image": "https:\/\/upload.wikimedia.org\/wikipedia\/commons\/6\/6e\/Auckland_City_from_Mt_Victoria%2C_Devonport_-_Flickr_-_111_Emergency_%281%29.jpg"
}
]

Lastly, here's my post request in Angular:

  $http.post('capitalData.php',                         
  {"capital":input1.val(),"country":input2.val(),
  "continent":input3.val(),"image":input4.val()}).success(function(){
  alert("Added!");
  });

Edit: Updated the post request to specify which data I'm sending but it still returns nulls.

adace1
  • 29
  • 9
  • You're not sending any data with your [post](https://docs.angularjs.org/api/ng/service/$http#post) in Angular. – segFault Nov 27 '16 at 23:27
  • Updated it. Still doesn't work though. – adace1 Nov 27 '16 at 23:48
  • Assuming you instantiated `input1`, `input2`, etc. correctly in angular, then your PHP code needs to be updated to use your new indices, eg. `capital` instead of `in1`... You may also want to `var_dump($_POST);` to see what you are getting server-side. – segFault Nov 27 '16 at 23:51
  • I'm getting the correct data and it manages to correctly update the JSON file but now the problem is that it only works when I use
    and take out the Angular post request (in fact, I can't even see what var_dump returns unless I do it that way). However, when I only use the Angular post request it doesn't work.
    – adace1 Nov 28 '16 at 00:19
  • And I changed the indices but it still doesn't work. – adace1 Nov 28 '16 at 00:36
  • It might help to show more of your Angular code in your question. Also for AJAX debugging I find it quite useful to use the Network tab in [Google Chrome DevTools](https://developers.google.com/web/tools/chrome-devtools/) it should show you the POST request and you can view the response which should display the var_dump. – segFault Nov 28 '16 at 00:43
  • Possible duplicate of [Angularjs - Form Post Data Not Posted?](http://stackoverflow.com/questions/20084549/angularjs-form-post-data-not-posted) – segFault Nov 28 '16 at 00:44

0 Answers0