2

How to save multiple checkbox value into database i have written a code below i cant understand how to pass the $scope.selection array into url please see below code and suggest me

<tr ng-repeat="result in results">
 <td <input type="checkbox" name="selectedadd" ng-model="addvalue" value="{{result.user_id}}" ng-checked="selection.indexOf(result.user_id) > -1" ng-click="toggleSelection(result.user_id)"> {{result.name}}</td>
</tr>
<td  ng-click="linkToSelectedPlayer()" value="{{result.user_id}}"> <i class=""></i> Selected Add</a></td>

$scope.selection=[];  

    $scope.linkToSelectedPlayer = function(){
    console.log($scope.selection);

   //Getting response with $scope.selection

   //Array[156,355,665,666]

    $http.post("v1/xyz/"+$scope.user.user_id+"/abc/"+$scope.selection)
        .success(function (response){

          $location.path('/home/'); 

        }).error(function (data) {
          alert(“invalid”);
        });

    // toggle selection for a given employee by id
    $scope.toggleSelection = function toggleSelection(userid) {
      var idx = $scope.selection.indexOf(userid);

      // is currently selected
      if (idx > -1) {
        $scope.selection.splice(idx, 1);
      }

      // is newly selected
      else {
        $scope.selection.push(userid);
      }
    };
  };

Thanking you in advance early reply will be highly apprecaited

1 Answer 1

2

When you call the $http.post() function you can pass 2 arguments: the URL and the post data like this:

$http.post(URL, data)

More over .success() and .error() functions are deprecated, you should use .then()

$http.post(URL, arrayObject)
.then(function successCallback(response) {
}, 
function errorCallback(response) {
}

Then in your server side controller you can get the post data like this (sorry it's java code, I don't know about php)

@RequestMapping( value="/URL",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void yourFunction(@RequestBody DataDto dataDto) {
}
Sign up to request clarification or add additional context in comments.

5 Comments

Deunz Thanks for this support but I am confused on this line $http.post("v1/xyz/"+$scope.user.user_id+"/abc/"+$scope.selection) because $scope.selection taking only first value (16054) but i am getting value in Array [ "16054", "8353", "9979" ] i would like to pass those value not single value how it is psbl
Hello, I don't think you can pass an array object through an URL. You should use some post data for this can't of types. SO transform the URL to [v1/xyz/"+$scope.user.user_id+"/abc]. Then you can pass your array as I told you in the reply.
So did you get over this ?
Yes Deunz it was helpful but somewhere in server side i m mistaking :(
Ah I am very sorry here I can't really help you much as I don't know about PHP, but you may find answers here : stackoverflow.com/questions/23308042/…

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.