AngularJS Question

Getting blank value in database using angular js

I have a contact form containing 3-4 fields. I am just trying to send only first field into database using Angular-js. But i am getting blank value in database.
Can you guys please check where is actually i am going wrong ?

contactForm

<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';
}
}

Answer

You need to change $scope.firstName in the function to $scope.userForm.firstName. As for now, $scope has no member named firstName, that's why it's empty in your function.

EDIT: Actually, you need $scope.user.firstName, $scope.userForm.firstName is angular model object and it not what is needed in this context.

Comments