-1

I'm trying to make a redirect but with post, to avoid all the parameters in the Url. I'm using Symfony 4.3 and PHP 7.2

Here's my code:

return $this->redirect($this->generateUrl('my-account', $arrayOfValues));

When I do this, GET is what is used. There's a way to make it fly via POST? Thanks

2
  • Why not save the values in a session then redirect? Commented Apr 28, 2020 at 15:02
  • This is not going to work. Redirect will always result in a GET. Please use forward. Commented Apr 28, 2020 at 15:04

2 Answers 2

-1

You would be better off putting the array in a session and then redirecting without GET parameters and then accessing the session variables in the next page.

You can POST with JavaScript, though this is from years ago and there is undoubtedly a better way now:

function http_post_redirect($url='', $data=array(), $doc=true) {

    $data = json_encode($data);

    if($doc) { echo "<html><head></head><body>"; }

    echo "
    <script type='text/javascript'>
        var data = eval('(' + '$data' + ')');
        var jsForm = document.createElement('form');

        jsForm.method = 'post';
        jsForm.action = '$url';

        for (var name in data) {
            var jsInput = document.createElement('input');
            jsInput.setAttribute('type', 'hidden');
            jsInput.setAttribute('name', name);
            jsInput.setAttribute('value', data[name]);
            jsForm.appendChild(jsInput);
        }
        document.body.appendChild(jsForm);
        jsForm.submit();
    </script>";

    if($doc) { echo "</body></html>"; }
    exit;
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's a great idea. Thanks it works perfectly!
Mark accepted if it solves your problem
-1

It's impossible to redirect a POST request because the browser would have to re-send the POST data (which it doesn't). What you should do instead in this case is use forwarding.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.