Display Message on redirect?
I'm making a page where you input info and click "Install" and then it goes to a file named "install_开发者_JS百科submit.php". I have it redirecting back to the form page when there is missing information display the message "Missing Information"!
this is my current code for "install_submit.php".
<?php
$dbname = $_POST['db_name'];
$dbuser = $_POST['db_user'];
$dbpass = $_POST['db_pass'];
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($dbname) || empty($dbuser) || empty($dbpass) || empty($username) || empty($password))
{
header("Location: install.php");
Echo("Missing Information!");
}else{
Echo("Success!");
}
?>
Save it in a session and display it in your form location.
$_SESSION['error_message'] = "Missing Information!";
And in your form location:
<p class="errors"><?php if (!empty($_SESSION['error_message']) echo $_SESSION['error_message']; ?></p>
you can change the url in header and pass a parameter for example
header("Location: install.php?submit=false");
then in your "install_submit.php" you can get the info
if($_GET['submit'] == 'false') {
echo("Missing Information!");
}
You need to use a session variable, like so:
session_start();
if(empty($dbname) || empty($dbuser) || empty($dbpass) || empty($username) || empty($password))
{
$_SESSION["error_message"] = "Missing Information!";
header("Location: install.php");
}else{
unset($_SESSION["error_message"]);
echo "Success!";
}
Then, on install.php reference the session variable somewhere like this:
<?php
if (isset($_SESSION["error_message"])) {
echo $_SESSION["error_message"];
}
?>
Don't do a redirect and don't use a separate url for processing. Handle the form submit on the same page. You don't want your user to provide all the information again.
You could redirect to install.php?error=true&db_name=...&db_user=...
but it's just more complicated.
After you redirect, you will no longer be able to output from the current page. This is why your echo("missing information!"); does not do anything at the moment.
You could try setting the error message in a session, and then check if the error-session is populated on the form page:
session_start();
$_SESSION['errmsg'] = "Missing information!";
header("Location: install.php");
and then in your install.php:
session_start();
if(isset($_SESSION['errmsg'])) {
echo $_SESSION['errmsg'];
unset($_SESSION['errmsg']);
}
精彩评论