Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want to save the value contained in multiple checkboxes only if they are checked. They would be saved into a MySQL database, using php. Here is my HTML code.

<div class="panel" ng-controller="checkBoxController"> 
    <input class="form-control" placeholder="Cherchez vos contacts" type="text" ng-model="searchText">
    </br>
    <div ng-repeat="employee in employees | filter:searchText ">
        <label class="action-checkbox">
            <input id="{{employee.name}}" type="checkbox" value="{{employee.name}}" ng-checked="selection.indexOf(employee.name) > -1" ng-click="toggleSelection(employee.name)">
        </label>
        <label for="{{employee.name}}"></label>
            {{employee.name}}
        </label>
    </div>
    </br>
    <span class="selected-item">Destinataires choisis:<span>
    <div ng-repeat="name in selection" class="selected-item">
        {{name}}
    </div>
</div>

Here is my JS code.

var myApp = angular.module('myApp', []);

myApp.controller('checkBoxController', function($scope, $http) {
    $http.get("information.php")
        .success(function (response) {
            $scope.employees = response.records;
        }); 

    $scope.selection = [];

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

        if (idx > -1) {
            $scope.selection.splice(idx, 1);
        } else {
            $scope.selection.push(employeeName);
        }
    };
});

What is the next step? Maybe save in a JSON file and send by HTTP post? I am lost.

share|improve this question
    
You used $http.get to get your employees data from php. You can use $http.post to send data to php. So basicly $http.post("myfile.php", {selection: $scope.selection}) for example. – Guinn Jul 9 '15 at 12:37
    
you can simply gather formdata and send it to the desired php file which is then doing the insert check out this fiddle jsfiddle.net/YGQT9 – stackg91 Jul 9 '15 at 12:40
    
Sorry, I tried but I receive noting in my php page. – WANG Qi Jul 9 '15 at 13:27

All you have to do is have an endpoint that can accept JSON and send your array to it, from a function in your controller.

$scope.sendSelection = function() {
    $http.post('your/url.php', { selection: $scope.selection })
        .success(function(data) {
            // Handle success.
        })
        .error(function(err) {
            // Handle error.
        });
}

Then have a button to send it in your HTML:

<button data-ng-click="sendSelection()">Save Selections</button>

Having the PHP file receive and process the JSON array is another thing altogether.

share|improve this answer
    
I have other information to send, so I use <form name="form" action="upload.php" method="post" enctype="multipart/form-data"> and I write <input class="btn btn-default" ng-click="sendSelection()" type="submit" value="Envoyer"/> to submit, but I still can't receive information of checkbox, here is my code php $data = json_decode(file_get_contents("php://input")); print_r ($data); is it wrong? – WANG Qi Jul 9 '15 at 13:18
    
What shows up when you submit the form? The output of print_r($data); should guide you in the right direction. – kiswa Jul 9 '15 at 13:25
    
It shows nothing. – WANG Qi Jul 9 '15 at 13:30
    
In the case of your form, you don't need the ng-click event on the submit button, since you're not using Angular to submit it. Either that, or you need to include everything from the form in that sendSelection function and use $http.post and see what the data is when it submits. – kiswa Jul 9 '15 at 13:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.