Skip to content Skip to sidebar Skip to footer

Redirect After Posting A Html Form

Is there anyway to redirect the user after posting a HTML form? I have a form which looks like this
if( isset( $_POST['answer'])) {
    header( 'Location: http://www.example.com/newurl');
    exit();
}

Note that you should be using a full URL in the header() call, and that you likely want to exit() after calling header() in this case.

Solution 2:

You can use a simple php header('yourlocation.php'); to move them away from the form.

The PHP that your form action pointed to will still continue executing even if your header() is at the top of the php file.

The header redirect will take care of the user hitting refresh - it has already moved them past the point of sending form data, so even if they refresh, it will just open the page again sans any form submissions.

An example is as fololows:

<?php/* This will give an error. Note the output
 * above, which is before the header() call */// Process Form Data, insert to database, whatever you want... then
header('Location: http://www.example.com/');
?>

The user then ends up at wwww.example.com and they can spam refresh as much as they like, but all the processing has already taken place. It won't resend the information (or process their payment again or anything else).

Solution 3:

Yes, use a redirect header:

header("Location: http://new.location.com/w00t");

This must be called before any output is sent.

Take note that everything after the header call would still be executed! So you'll need die() or exit() afterwards to prevent unexpected results.

Solution 4:

You can just simply specify URL to which form will go after being submitted.

<formaction="http://localhost/redirected.php"method="post">

You can check with PHP whether form is submitted and then redirect:

if (isset($_POST['answer']))
    header('Location: redirect.php');

And You can achieve this with javascript/jquery.

$('#submit_button').click(function () {
    // do everything with variableswindow.location = 'redirect.php';
});

Solution 5:

Yep, just post the data using ajax and then use window.location = 'http://xxxx.com' to redirect user to location of your choice. That's assuming you want to redirect user without using server-side location headers (like the other ones posted).

Post a Comment for "Redirect After Posting A Html Form"