PHP Redirect to Another Page After Form Submit

Redirect User after Form Submit

What <?php $_PHP_SELF ?> means is that the current file, that is the file containt your form gets called on form submit. So, you can keep that the same and then add the redirect code at the top of the page and trigger it if the form is submitted. Note that a header redirect must be called before any other output - html for example.

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//add any other checks such as form input values
header('Location: http://myhost.com/mypage.php');
exit;
}

or change you form action to any other .php you want to load and handle the form inputs there.

<form method="post" action="some_other.php" name="interactionForm">

UPDATE:

The header() needs to go before any output so:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//add any other checks such as form input values
header('Location: http://myhost.com/mypage.php');
exit;
}
?>
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>New Interaction</title> <!--Declare CSS and JavaScript-->
<link rel="stylesheet" type="text/css" href="RealtyCRM_Style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.resmenu.min.js"></script>
</head>

You can also update the php settings to view errors. This will tell you specifically if you're outputting data before the redirect - among other things but that would be among your first warnings. Just make sure to remove the ini_set() when you're done

<?php
ini_set('display_errors', '1');
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//add any other checks such as form input values
header('Location: http://myhost.com/mypage.php');
exit;
}
?>

Redirect another page after successful form submission

Replace header("Location:http://www.google.com");

with:

window.location.href = 'https://www.google.com';

header() is a PHP function which means "before you send the response back to the client (browser), add the Location: header. This header tells chrome to redirect using a 301 or 302 status code. PHP code obviously won't work in Javascript.

Considering javascript ONLY happens in the browser there are no headers that need to be set. You can just tell javascript to directly navigate the current window to the URL you specify.

Redirect after submitting a form (HTML, PHP)

First, remove the inline handler on your submit button. Your HTML should look like so:

<form method="post" action="" id="formwrap">
<!-- questions here -->
<input id="submitButton" class="block" type="submit" name="verzenden" value="Opslaan" />
</form>

Then, update your JavaScript to add an event handler that targets your formwrap element's submit event. Then prevent the form from making a post-back with preventDefault. Once the default behavior is prevented, you can send the user to whatever url you want :)

document
.getElementById("formwrap")
.addEventListener("submit", function(e) {
e.preventDefault();
window.location.href = "test.php";
});

PHP page not redirecting after form submission?

The problem was it is not checking to see if the form was used before executing the statements. Try this:

<?php
if(!empty($_POST)){
$connection = mysqli_connect("localhost", "widget_cms", "password", "widget_corp");
if(mysqli_connect_errno()){
die("Database connection failed: " . mysqli_connect_error() . "(" . mysqli_connect_errno() . ")");
}
$username = $_POST['username'];
$username = mysqli_real_escape_string($connection, $username);
$query = "INSERT INTO subjects ( menu_name, position, visible ) VALUES ('{$username}', 4, 1)";
$submission = mysqli_query($connection, $query);
if($submission){
header("Location: second_page.php");
}
if(!$submission){
die("Error: " . mysqli_error($connection, $submission));
}
}
?>
<html>
<body>
<form method="post" action="login.php" method="post">
Username: <input type="text" name="username"><br>
<input type="submit" name="submit" id="dlbutton"><br>
</form>
</body>
</html>


Related Topics



Leave a reply



Submit