AngularJS和PHP在数据库中输入空行

时间:2015-06-11 04:55:44

标签: php mysql angularjs database

我试图找出我的PHP文件没有收集数据的原因。 这是我的index.html

JsonValueProvider

这是我的script.js

<!DOCTYPE html>
<html lang="en" ng-app="gemStore">
<head>
    <meta http-equiv='Content-Type':'application/x-www-form-urlencoded; charset=UTF-8' />
    <title>Angular</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div ng-controller="custController">
        <form>
            First Name: <input type="text" ng-model="firstname"><br>
            Last Name: <input type="text" ng-model="lastname"><br>
            Choose Username: <input type="text" ng-model="username"><br>
            Choose Password: <input type="text" ng-model="password"><br>
            Address 1: <input type="text" ng-model="address1"><br>
            Address 2: <input type="text" ng-model="address2"><br>
            City: <input type="text" ng-model="city"><br>
            State: <input type="text" ng-model="state"><br>
            Zip: <input type="number" ng-model="zip"><br>
            <input type="button" value="Submit" ng-click="insertdata()"><br>
        </form>
    </div>

    <script src="js/angular.min.js"></script>
    <script src="js/script.js"></script>
    <script src="js/products.js"></script>
</body>
</html>

最后是我的PHP文件

(function(){
    var app = angular.module('gemStore', ['store-products']);
    app.controller('StoreController', ['$http', function($http){
        var store = this;
        store.products = [];
        $http.get('/store-products.json').success(function(data){
            store.products = data;
        });
    }]);

    app.controller('custController', function($scope,$http){
        $scope.insertdata = function(){
            $http.post('insert.php',{'firstname':$scope.firstname,'lastname':$scope.lastname,'address1':$scope.address1,'address2':$scope.address2, 'city':$scope.city,'state':$scope.state,'zip':$scope.zip,'username':$scope.username,'password':$scope.password})
            .success(function(data,status,headers,config){
                console.log('data inserted successfully. First Name: ' + $scope.firstname);
            });
        }
    });
})();

我认为它与标题有关,但我不知道应该在哪里添加它或需要更改的内容。任何帮助,将不胜感激。感谢。

另外,我使用角度1.3.16,但我怀疑这有所不同。

1 个答案:

答案 0 :(得分:1)

在insertdata函数中尝试此操作。

$ scope.insertdata = function(){

var data = {'firstname':$scope.firstname,'lastname':$scope.lastname,'address1':$scope.address1,'address2':$scope.address2, 'city':$scope.city,'state':$scope.state,'zip':$scope.zip,'username':$scope.username,'password':$scope.password};
    $http({
             url:'insert.php', 
             method: 'post',
             headers: {'Content-Type': 'application/x-www-form-urlencoded'},
             transformRequest: function(obj) {
                    var str = [];
                    for(var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    return str.join("&");
               },
             data :data
            }).success(function(data,status,headers,config){
            console.log('data inserted successfully. First Name: ' + $scope.firstname);
            });

}