在重定向时显示消息?

发布于 2024-11-29 04:27:19 字数 515 浏览 1 评论 0原文

我正在制作一个页面,您可以在其中输入信息并单击“安装”,然后它会转到名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恍梦境° 2024-12-06 04:27:19

将其保存在会话中并将其显示在您的表单位置。

$_SESSION['error_message'] = "Missing Information!";

并在您的表单位置:

<p class="errors"><?php if (!empty($_SESSION['error_message']) echo $_SESSION['error_message']; ?></p>

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>
遇见了你 2024-12-06 04:27:19

您可以更改标头中的网址并传递参数,例如

header("Location: install.php?submit=false");

然后在您的“install_submit.php”中您可以获得信息

if($_GET['submit'] == 'false') {
  echo("Missing Information!"); 
}

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!"); 
}
绳情 2024-12-06 04:27:19

您需要使用会话变量,如下所示:

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!";
}

然后,在 install.php 上引用会话变量,如下所示:

<?php 
if (isset($_SESSION["error_message"])) {
    echo $_SESSION["error_message"];
}
?>

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"];
}
?>
溺渁∝ 2024-12-06 04:27:19

不要进行重定向,也不要使用单独的 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.

呆° 2024-12-06 04:27:19

重定向后,您将无法再从当前页面输出。这就是为什么你的 echo("missing information!");目前不执行任何操作。

您可以尝试在会话中设置错误消息,然后检查错误会话是否填充在表单页面上:

session_start();
$_SESSION['errmsg'] = "Missing information!";
header("Location: install.php");

然后在 install.php 中:

session_start();
if(isset($_SESSION['errmsg'])) {
echo $_SESSION['errmsg'];
unset($_SESSION['errmsg']);
}

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']);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文