0

This is my php script

    switch($x[0]){
        case 'categorias' :
                    $_POST['step'] = 'categorias';
                    $_POST['pais']=$_POST['pais'];
                    $_POST['method']=1;
                    ?><script>
                    window.location.href = "<?php echo $url ?>?step=categorias";
                    </script><?
                    break;
        case 'corredores':
                    $_POST['step'] = 'corredores';
                    $_POST['pais']=$_POST['pais'];
                    $_POST['method']=1;
                    ?><script>
                    window.location.href = "<?php echo $url ?>?step=corredores";
                    </script><?
                    break;
        default: //do something ;
}

When I am trying to do this I am loosing the values that I am sending using POST[''] variable ,

Can any one help me out with this , I tried using the php header() option but it is producing header already sent error,

So i used the javascript

Can any one help me how I can fix this

Thanks in advance

Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130
  • 4
    You need to repost these values using a form. Or you can use Session or cookies. – Muhammad Bilal Dec 31 '14 at 11:26
  • 2
    Firstly, you shouldn't generally be _setting_ `$_POST` values. Second, even if that whole concept worked, `window.location.href` performs a `GET` request. You need to either put the values into `$url` or output a form with the required POST values in it and submit the form with javascript – Rhumborl Dec 31 '14 at 11:26
  • @madforstrength okay so I need to add an HTML form tag inside the case statement and send it , this is what you mean ? . – Vikram Anand Bhushan Dec 31 '14 at 11:27
  • you will loose it as `window.location.href` works like you browse any page by just passing URI in address bar. You can pass your data in querystring if it is not big – vikas Dec 31 '14 at 11:27
  • If you use form than you need to submit it programatically instead of using `location.href`. for example `document.form1.submit();` – Muhammad Bilal Dec 31 '14 at 11:28
  • @vikas I am trying to modify a application that has 2000 lines of code they have used $_Post[''] and validated most of the part changing all to get will be a big task , :( – Vikram Anand Bhushan Dec 31 '14 at 11:29
  • then you should perform a POST operation – vikas Dec 31 '14 at 11:30

2 Answers2

2

already answerd here jQuery - Redirect with post data

var redirect = 'Your Url here';
$.redirectPost(redirect, {x: 'example', y: 'abc'}); // your parametters

// jquery extend function
$.extend(
{
    redirectPost: function(location, args)
    {
        var form = '';
        $.each( args, function( key, value ) {
            form += '<input type="hidden" name="'+key+'" value="'+value+'">';
        });
        $('<form action="'+location+'" method="POST">'+form+'</form>').appendTo('body').submit();
    }
});
Community
  • 1
  • 1
Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48
1

As stated in the comments, $_POST should not be set manually but generated by submitting a form. You can add the values to the URL like this:

switch($x[0]){
    case 'categorias' :
                $step = 'categorias';
                $pais = $_POST['pais'];
                $method = 1;
                ?><script>
                window.location.href = "<?php echo $url ?>?step=$step&pais=$pais&method=$method";
                </script><?
                break;
    case 'corredores':
                $step = 'corredores';
                $pais = $_POST['pais'];
                $method = 1;
                ?><script>
                window.location.href = "<?php echo $url ?>?step=$step&pais=$pais&method=$method";
                </script><?
                break;
    default: //do something ;

If you have to use $_POST in the rest of your code you could do something like this at the $url where it is sent.

$_POST['pais'] = $_GET['pais'];

and so on. Or, replace your $_POST references with $_REQUEST.

RST
  • 3,899
  • 2
  • 20
  • 33