使用角度js在数据库中获取空白值

时间:2016-09-21 09:09:12

标签: javascript php angularjs

我有一个包含3-4个字段的联系表格。我只是尝试使用Angular-js将第一个字段发送到数据库。但我在数据库中得到空白值。 请你们检查一下我哪里出错了?

联系形式

<div ng-app="contactApp" ng-controller="contactController">
<div class="contact-grids">
<div class="col-md-12 contact-para">
<form name="userForm" ng-submit="submitForm()" novalidate>
<div class="grid-contact">
<div class="col-md-6 contact-grid" ng-class="{'has-error':userForm.firstName.$invalid && !userForm.firstName.$pristine}">

<p>First Name</p>       
  <input type="text" name="firstName" ng-model="user.firstName" required>
  <p ng-show="userForm.firstName.$invalid && !userForm.firstName.$pristine" class="help-block">You name is required.</p>
</div>

<div class="send">
<input class="btn btn-danger" type="submit" value="Send" ng-disabled="userForm.$invalid">
</div>
</form>


 <script>
    var myApp = angular.module('contactApp',[]);
    myApp.controller('contactController',function($scope,$http){

        $scope.submitForm = function()
        {
            if($scope.userForm.$valid)
            {

                  $http.post(
                          "contactFormProcess.php",
                  {'firstName':$scope.firstName}
                          ).success(function(data){
                              alert(data);
                               $scope.firstName = null;  
                          });

            }
        };

   });

</script>       

contactFormProcess.php

 $data = json_decode(file_get_contents("php://input"));  
 if(count($data) > 0)  
 {  

    $first_name = mysqli_real_escape_string($conn, isset($data>firstName));       
      $query = "INSERT INTO contactform(firstName) VALUES ('$first_name')";  
      if(mysqli_query($conn, $query))  
      {  
           echo "Data Inserted...";  
      }  
      else  
      {  
           echo 'Error';  
      }  
 }  

1 个答案:

答案 0 :(得分:1)

您需要将函数中的$scope.firstName更改为$scope.userForm.firstName。至于现在,$scope没有名为firstName的成员,这就是为什么它在你的函数中是空的。

编辑:实际上,您需要$scope.user.firstName$scope.userForm.firstName是角度模型对象,而不是此上下文中所需的内容。