Trying to access the data I’ve sent through POST method to another file in PHP

  Kiến thức lập trình

So, I am creating an e-commerce website, where i want the cart data to be sent to a separate php file using POST method. I’m using the $http service of angularJS for this. The problem is that when i add a link to the cart page, it apparently expects get data, which i cannot provide, and thus there is no data for me to display. Is there a way to work around this?

This is how my angularJS function looks like:

app.controller('myCtrl', function($scope, $http) {
    $scope.executefunc = function(name, price) {
        var data = {
            action: 'setcart',
            bname: name,
            bprice: price
        };

        $http.post('cart.php', data)
            .then(function(response) {
                alert('Response: ' + JSON.stringify(response.data));
            }, function(error) {
                console.error('Error:', error);
                alert("Failed to add to cart. Please try again.");
            });
    };
});

Here, in the alert box, I get the correct response, but when i access the page using a hyperlink, I cannot access the data.
here is what my PHP file looks like:

<?php
session_start();
$json = file_get_contents('php://input');
$values = json_decode($json, true);
if($values!= NULL){
var_dump($values);
}
else {
    echo "Data not received";
}

is there anything i can do here?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT