2

Now I have a php page "userreq.php" managed by a angular js controller.On a button click I am calling a funcion which posts the data to another php page "checkout.php".

$http({
        url: "checkout.php",
        method: "POST",
        data: {
            data: variable
        }
    }).success(function(response) {
    console.log(response);
});

This is how I am posting it.Now I want the page to navigate to checkout.php after the variable has been posted so that I can work with that variable in checkout.php. How can I achieve that.

Gopal Chandak
  • 387
  • 7
  • 19
  • you could use window.location.href = 'url'; in your success function. https://developer.mozilla.org/en-US/docs/Web/API/Window/location – Nitsan Baleli Feb 14 '16 at 08:31
  • but it is redirection right http post will have no effect..I am new to angular so I may be wrong – Gopal Chandak Feb 14 '16 at 09:14
  • depends on what you want, you could simply pass params with window.location instead of using ajax – Nitsan Baleli Feb 14 '16 at 09:16
  • $scope.checkout = function(){ $http({ url:"checkout.php", method : 'POST', data :{ total:$scope.total }, headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} }).success(function(data, status, headers, config) { window.location.href="checkout.php"; }).error(function(data, status, headers, config) { console.log("not done"); }); } and in checkout.php i am doing but i am getting error that total is not defined – Gopal Chandak Feb 14 '16 at 09:20
  • could you provide me with example of passing params with window.location.href – Gopal Chandak Feb 14 '16 at 09:24
  • here: http://stackoverflow.com/a/12527279/2460773 – Nitsan Baleli Feb 14 '16 at 09:25
  • 1
    thanks for the help sir..but actually i need to pass an array – Gopal Chandak Feb 14 '16 at 09:33

1 Answers1

0

Add the $httpParamSerializerJQLike in your controller and pass your data as query parameters.

Example :

var url = 'checkout.php'; 
var data = { id: 1, name: 'foo' };

$http({
    url: url,
    method: "POST",
    data: data
})
.success(function(response) {
    console.log(response);
    window.location.href = url + '?' + $httpParamSerializerJQLike(data);
});

And in your php, retrieve the data from $_GET :

$field = $_GET[$field];
// Output: array( 'id' => 1, 'name' => 'foo' )
chalasr
  • 12,971
  • 4
  • 40
  • 82
  • If you want use POST rather than GET see http://stackoverflow.com/questions/5576619/php-redirect-with-post-data – chalasr Feb 15 '16 at 11:38