1

I had a problem with multiple insert in angularJS. $scope.tes from:

<input type="text" ng-model="tes" name="tes"/><br>

Here's my app.js:

$scope.data = [{matapelajaran : '', dosen: ''}];
$scope.tambah = function(){
            $scope.data.push({matapelajaran : '', dosen: ''});
$scope.cancel = function(){
            $scope.data.splice($scope.data.length-1);
        };

    $scope.simpan = function(){
        $http.post('http://localhost/bismillah/server.php?action=update',$scope.data,{"tes":$scope.tes}).success(function(response){
            if(response > 0){
                $scope.data = [{matapelajaran: '', dosen: ''} ];
                $scope.pesan = response +' Data telah berhasil di simpan';
                $scope.$apply();
            }
            });
        }

And here's my server.php

<?php

$conn = new Mongo();
$db = $conn->selectDB('apaan');


switch($_GET['action']){
    case 'insert' :
        $data = json_decode(file_get_contents("php://input"));
        $tes = $data->tes;
        $query = $db->siswa;
        $query->insert(array("tes"=>$tes));
    case 'update' :
        $data = json_decode(file_get_contents("php://input"));
        $tes = $data->tes;
        $i = 0;
        $query = $db->siswa;
        $query->insert(array("tes"=>$tes));
        foreach($data as $k => $v){
            $query->update(array("tes"=>$tes),array('$addToSet'=>array("matapelajaran"=>array("matapelajaran"=>$v->matapelajaran, "dosen"=>$v->dosen))));
            $i++;
        };
        echo json_encode($i);
        break;
}

Both of those codes couldn't work for me. I don't know with this part:

$http.post('http://localhost/nothing/server.php?action=update',$scope.data,{"tes":$scope.tes}).success(function(response){

Please help me..

3
  • Could you still more elaborate like, what's the sample json inside $scope.data and is {tes"$scope.tes} a key to identify a record ? Commented Jan 27, 2017 at 13:25
  • Could you please look at my post again ? It has explained bout sample json inside both of $scope.data and {"tes":$scope.tes} Commented Jan 27, 2017 at 14:00
  • stackoverflow.com/a/32304405/7203487 You may check this. Commented Jan 27, 2017 at 14:36

0

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.