在重定向时显示消息?
我正在制作一个页面,您可以在其中输入信息并单击“安装”,然后它会转到名为“install_submit.php”的文件。当缺少信息时,我将其重定向回表单页面,显示消息“缺少信息”!
这是我当前的“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!");
}
?>
I'm making a page where you input info and click "Install" and then it goes to a file named "install_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!");
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将其保存在会话中并将其显示在您的表单位置。
并在您的表单位置:
Save it in a session and display it in your form location.
And in your form location:
您可以更改标头中的网址并传递参数,例如
header("Location: install.php?submit=false");
然后在您的“install_submit.php”中您可以获得信息
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
您需要使用会话变量,如下所示:
然后,在 install.php 上引用会话变量,如下所示:
You need to use a session variable, like so:
Then, on install.php reference the session variable somewhere like this:
不要进行重定向,也不要使用单独的 url 进行处理。在同一页面上处理表单提交。您不希望用户再次提供所有信息。。
您可以重定向到
install.php?error=true&db_name=...&db_user=...
但它更复杂。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.重定向后,您将无法再从当前页面输出。这就是为什么你的 echo("missing information!");目前不执行任何操作。
您可以尝试在会话中设置错误消息,然后检查错误会话是否填充在表单页面上:
然后在 install.php 中:
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:
and then in your install.php: