0

The html code below:- ` AngularJS | $http server

<div ng-controller="people">
    <ul>
        <h2> Names and Ages of progrmmers: </h2>
        <li ng-repeat="person in persons">
            {{person.Name + ':' + person.Age}}
        </li>
    </ul>

</div>

The js file below :

var app=angular.module('mainApp',[]);

app.controller('people',function($scope,$http){
    $http({
        method: 'POST',
        url: 'http://localhost/AngularJS/post.json',
        data:{"Name": "1234", "Age":"13","Fav_Color":"black"},
        headers:{'records':'json'}
    })
    .success(function(response){
        $scope.persons=response.records;
    });
});

I need the data to be saved in a file name "post.json". But it is not working. So anyone could tell what is wrong with my code.

Debug Diva
  • 26,058
  • 13
  • 70
  • 123

2 Answers2

0

make a php file that will save data into the database

put this inside your php file, and update column names and add the query, then call this file using your ajax request as you have written above

<?php 

// get all the values from the request
$name = $_POST['Name'];
$age = $_POST['Age'];
$favcolor = $_POST['Fav_Color'];

if(!$name || empty($name))
{
  echo "name is required";
}

if(!$age || empty($age))
{
  echo "age is required";
}

if(!$favcolor || empty($favcolor))
{
  echo "favorite color is required";
}

// change according to column names on the left side
$item = array(
  "name" => $name,
  "age" => $age,
"favcolor" => favcolor
);

// save in mongo db - ref link : http://php.net/manual/en/mongo.queries.php

echo "success";

?>
  • Donot want to store in data base.. what so ever the user enter the data it must get stored in a file in our computer – user7339499 Dec 25 '16 at 12:32
  • what type of file you want, txt file or excel file or other extension ? file.txt, file.xls/file.xlsx etc .txt is not good option as it is hard to update data, excel is also same, access file is a good option if you don't want to use database – Zain Ullah Muhammad Dec 25 '16 at 12:44
  • access file. What does that mean? – user7339499 Dec 25 '16 at 12:46
  • when you install ms office pack, you will get these applications ms word ms excel ms access outlook etc in these applications, the ms access is like a database, but is a separate file – Zain Ullah Muhammad Dec 25 '16 at 13:04
  • oh yeah! so how could i save access file when user enter data – user7339499 Dec 25 '16 at 13:06
  • use the same php file i gave you.. then, where i have commented " // save in mongo db" use this tutorial to create the access file, and then choose the insert query and put it under my comment "// save in mongo db" thats it, then test it – Zain Ullah Muhammad Dec 25 '16 at 13:11
  • https://www.sitepoint.com/using-an-access-database-with-php/ tutorial link – Zain Ullah Muhammad Dec 25 '16 at 13:11
0

You can't write to a file with javascript it would be a security risk. For your POST call to work, you need a service and service's method to get the data and save it to a file. You can write your service in any language PHP, C# or JS.

Check this tutorial to create a restful api with NodeJS (since you are already working with javascript):https://www.thepolyglotdeveloper.com/2015/10/create-a-simple-restful-api-with-node-js/

Check this other tutorial to learn how to save data to a file with NodeJS: https://docs.nodejitsu.com/articles/file-system/how-to-write-files-in-nodejs/

This answer in stackoverflow shows you both how to create a service and how to save data to file (using NodeJS and ExpressJS): Using POST data to write to local file with node.js and express

**You want to make your $http call in an angular service not in your controller.

Once you create your service, change your code like this (I am using a restful api):

var app=angular.module('mainApp',[]);

app.controller('people',function($scope,$http){
    $http({
        method: 'POST',
        url: 'http://localhost/api/user',
        data:{"Name": "1234", "Age":"13","Fav_Color":"black"}
    })
    .success(function(response){
        $scope.persons=response.records;
    });
});
Community
  • 1
  • 1
coderdark
  • 1,481
  • 14
  • 30