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
How to Remove Array Element and Then Re-index Array
How to Detect Duplicate Values in PHP Array
How to Remove the Querystring and Get Only the Url
"Smtp Error: Could Not Authenticate" in PHPmailer
How to Resolve Curl Error (7): Couldn't Connect to Host
Filter Multidimensional Array Based on Partial Match of Search Value
How to Only Allow Certain Filetypes on Upload in PHP
Remove Script Tag from HTML Content
PHP Array to Json Array Using Json_Encode();
PHP $_Server['Http_Host'] Vs. $_Server['Server_Name'], Am I Understanding the Man Pages Correctly
Creating PHP Class Instance With a String
Trying to Get Property of Non-Object - Laravel 5
How Can Strip Whitespaces in PHP'S Variable
MySQLi Fetch_All() Not a Valid Function
PHP Sort Array by Two Field Values
Why Is Using a MySQL Prepared Statement More Secure Than Using the Common Escape Functions