0

I am trying to pass a php array in ng-click function to have it in php controller.

Here is my code I am passing php array to angular function.

 <button type="button" class="btn btn-success" 
  ng-click="removeItem('<?php echo  htmlspecialchars(json_encode($value['BookingsHotel']));?>')">Cancel Room</button>

here is my code, i am accessing my posted data with angularjs service

$booked_rooms=htmlspecialchars_decode($this->request->data['rooms']);
$booked_rooms=json_decode($this->request->data['rooms']);

once we are doing json decode it gives a null value.

3
  • Can you please explain further where the code you included are in your app? Can you include code for the $scope.removeItem function? Commented Aug 12, 2016 at 15:52
  • Also note that the $booked_rooms=json_decode(...); is acting on the original request data. You might want: $booked_rooms=json_decode($booked_rooms); if you want it to act on the result of htmlspecialchars_decode(...). Commented Aug 12, 2016 at 15:54
  • Hello @Pravin - would you mind accepting my answer if it helped you? Thanks! Commented Jul 2, 2018 at 14:42

1 Answer 1

2

Since you're using angular. Just use $http to communicate with your PHP backend.

$http({method: 'POST', url: your_url_variable, contentType: 'application/json; charset=utf-8',
        data: {var1:angular_var1, var2:angular_var2},
        headers: { 'Content-Type': 'application/json; charset=utf-8' }
    }).success(function (result) {
        console.log("result", result);
    }).error(function (data, status, headers, config) {
        console.log('ERROR', data, status, headers, config);
    });

In your php page...

if($_SERVER["REQUEST_METHOD"] == "POST") {
        $postdata = file_get_contents("php://input");
        if(!empty($postdata)) {
            $post = json_decode($postdata);

            $var1 = $post->var1;
            $var2 = $post->var2;

            // do some stuff, then echo the response as json back to your app.

            $result = ["success" => true; "message" => "This is a test message"];
            header('Content-Type: application/json');
            echo json_encode($result);
        }
}
Sign up to request clarification or add additional context in comments.

Comments

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.