0

I'm new to Angular and I don't know to send multiple checkbox value to PHP (Server-side).

Code: https://jsfiddle.net/7uLxhnaj/1/

HTML

<form name="EnqFrm" ng-app="EnqFrm" ng-controller="EnqController" ng-submit="save()">
<label>Class Days:</label>
<input name="chkweek" ng-model="formData.chkweek.Mo" type="checkbox">Mo
<input name="chkweek" ng-model="formData.chkweek.Tu" type="checkbox">Tu
<input name="chkweek" ng-model="formData.chkweek.We" type="checkbox">We
<input name="chkweek" ng-model="formData.chkweek.Th" type="checkbox">Th
<input name="chkweek" ng-model="formData.chkweek.Fr" type="checkbox">Fr
<input name="chkweek" ng-model="formData.chkweek.Sa" type="checkbox">Sa
<button id="btnSubmit" class="btn btn-default">SUBMIT</button>

AngularJS:

/* ANGULAR JS */
var weeksknm;
var app = angular.module('EnqFrm', []);
app.controller('EnqController', function ($scope, $http) {

    $scope.formData = {};

    $scope.save = function () {
        var url = "Controller/enq_postadmin.php";
        $http.post(url, $scope.formData).success(function (da) {
            alert(da);
            $scope.formData = "";
        });
    };

});

PHP:

<?php if ($_SERVER['REQUEST_METHOD']=='POST') {
    $data=file_get_contents("php://input");
    $inp=json_decode($data);
    echo $txtWeek=$inp->chkweek;
}
else {
    echo'try agian';
}
?>
Ajay Kumar Ganesh
  • 1,838
  • 2
  • 25
  • 33
Sujeet Jaiswara
  • 85
  • 1
  • 3
  • 13

2 Answers2

0

I have updated your code here

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

app.controller('EnqController', function ($scope, $http) {

$scope.formData = {};

$scope.save = function (da) {
    var url = "Controller/enq_postadmin.php";
    $http.post(url, da).success(function (response) {

         // Your action after getting response from server

    });     
    console.log(da);
};

});

HTML

<form name="EnqFrm" ng-app="EnqFrm" ng-controller="EnqController" ng-submit="save(formData.chkweek)">

Console.log Output

Object {Mo: true, Tu: true, We: true}

Pass this and get the key across.

Other way around is you need to use ng-true-value

 <input name="chkweek" ng-true-value="Monday" ng-model="formData.chkweek.mo" type="checkbox">Mo

And your output will be

Object {mo: "Monday"}
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
0

Tested working code on my local server. You could later use the $selectedweek to store it in your database.

PHP

<?php
/*Receive Files*/
$data=file_get_contents("php://input");
$inp=json_decode($data);
/*Convert Object to array*/
foreach($inp as $weekcode => $weekname){
$selectedweek[$weekcode] = $weekname; 
};
/*Pass the selected week array back to angularjs for confirmation*/
print_r($selectedweek);

HTML

<form name="EnqFrm" ng-app="EnqFrm" ng-controller="EnqController" ng-submit="save(formData.chkweek)">
        <label>Class Days:</label>
        <input name="chkweek"  type="checkbox" ng-true-value="'Monday'"  ng-model="formData.chkweek.Mo">Mo
        <input name="chkweek"  type="checkbox" ng-true-value="'Tuesday'" ng-model="formData.chkweek.Tu">Tu
        <input name="chkweek" type="checkbox" ng-true-value="'Wednesday'" ng-model="formData.chkweek.We">We
        <input name="chkweek" type="checkbox" ng-true-value="'Thursday'" ng-model="formData.chkweek.Th">Th
        <input name="chkweek" type="checkbox" ng-true-value="'Friday'" ng-model="formData.chkweek.Fr"> Fr
        <input name="chkweek" type="checkbox" ng-true-value="'Saturday'" ng-model="formData.chkweek.Sa">Sa
        <button id="btnSubmit" class="btn btn-default">SUBMIT</button>
    </form>


    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
    <script>

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

        app.controller('EnqController', function ($scope, $http) {

        $scope.formData = {};

        $scope.save = function (da) {
        var url = "Controller/enq_postadmin.php";
        $http.post(url, da).success(function (response) {
        console.log(response); // outputs your response from the server
        });     
        console.log(da); // outputs your selection passed from html to controller
        };

        });

    </script>
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83
  • but how to add to array value to PDO statement – Sujeet Jaiswara May 07 '15 at 06:37
  • public function InsertEnqueryByAdmin($txtWeek) { try { $this->sql = "INSERT INTO enquirybyadmin(" ClassWeek) " . "VALUES(?)"; $stmt = $this->conn->prepare($this->sql); $stmt->bindParam(1, $txtWeek, PDO::PARAM_STR); $stmt->execute(); return "Form Successfully Submited!"; } catch (PDOException $ex) { echo $ex->getMessage(); } } – Sujeet Jaiswara May 07 '15 at 06:40
  • http://stackoverflow.com/questions/4587988/how-to-pass-an-array-of-rows-to-pdo-to-insert-them – Alaksandar Jesus Gene May 08 '15 at 16:02