How to pass data from data binded form


#1

Hi everyone, i try to explain my issue and i hope i can do it. I try to post data with hidden text inputs to my server (PHP). I use this for data binding :

<form ng-submit="addfavourite()">
      <input type="hidden" ng-model="data.mac_id" ng-value="veri.mac_id">
      <input type="text" ng-model="data.user_id" ng-value="myuser">
      <input type="submit" value="" class="favourite">
  </form>

And i try to get data into my controller like this :slight_smile:

 $scope.data = {};

$scope.addfavourite = function(){
    
    var links = 'http://www.website.com/api.php';
    $http.post(links, {user_id : $scope.data.user_id, mac_id : $scope.data.mac_id}).then(function (res){
        $scope.response = res.data;
        if ($scope.response == 1) {
          $scope.messages = 'Eşleşme sağlandı.';
        }
        else if ($scope.response == 0)
        {
          $scope.messages = 'Eşleşme sağlanmadı.';
        }
    });
};

But i get this error :

ionic.bundle.js:26794 SyntaxError: Unexpected token E in JSON at position 0
    at fromJson (ionic.bundle.js:14655)
    at defaultHttpResponseTransform (ionic.bundle.js:23675)
    at ionic.bundle.js:23766
    at forEach (ionic.bundle.js:13691)
    at transformData (ionic.bundle.js:23765)
    at transformResponse (ionic.bundle.js:24554)
    at processQueue (ionic.bundle.js:29127)
    at ionic.bundle.js:29143
    at Scope.$eval (ionic.bundle.js:30395)
    at Scope.$digest (ionic.bundle.js:30211)

So i found the reason to see this error. I bind data into inputs with ng-value. And if i write something into the textbox manually it works. But it doesn’t work with only ng-value datas and get this error.

So how can i get this values from the form into my controller for post ot my server?

Can anyone help about this please.


#2

@parabahis You need to assign the value of "data.mad_id and “data.user_id” using $scope if you want to set it initially!

$scope.data.user_id = myuser;
$scope.data.mac_id = veri.mac_id;

Ng-value is mainly used on input[radio] and option elements, so that when the element is selected, the ngModel of that element (or its select parent element) is set to the bound value. It is especially useful for dynamically generated lists using ngRepeat, as shown below.

It can also be used to achieve one-way binding of a given expression to an input element such as an input[text] or a textarea, when that element does not use ngModel.

https://docs.angularjs.org/api/ng/directive/ngValue


#3

thanks for the reply. issue was solved by you. very thanks.