1

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';  
      }  
 }  
2
  • Have you checked the value of $scope.firstName just before $http.post(..)? Is it empty or not? Also, could you provide the form code? Commented Sep 21, 2016 at 9:11
  • @JakubJankowski See my updated code Commented Sep 21, 2016 at 9:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

hey ! I am getting integer 1 in database not string name ??
Well, that may be related to your PHP code and I have no idea about PHP. Can you check if $data variable has correct value? (the one sent from JavaScript)
Actually the issue is, if i send the value to php like this $scope.userForm.firstName ...... then it sends a integer value to php script so php script giving error as it does not accepting integer value there. ...... If i just write this $scope.firstName, now php script runs but enter a blank value
@bc110402307SyedZeeshanHaide look at my answer, I have edited it.
I was just going to tell you that i have recent sort it out by changing $scope.userForm.firstName to $scope.user.firstName ... lol
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.