如何获取表单提交状态

发布于 2024-12-10 18:30:10 字数 496 浏览 0 评论 0原文

我正在寻找一种“整洁”(干净)的方式来在提交表单后显示状态消息。

例如,如果用户更新他们的个人资料,我想显示一个带有文本“您的个人资料已更新!”的 DIV。

现在我正在这样完成此操作:

在发布数据并插入或更新数据库表后,我执行 header("Location: updateprofile.php?status=updated");

这工作得很好,但它不是很干净。我不需要查询字符串。

我尝试的另一种方法是设置变量 $subscribed=1;并且之后不进行重定向。但是,当用户更新其信息时,它仍然在表单字段中显示旧信息,直到刷新。所以这也行不通。

我知道我可以使用 AJAX 提交表单并以这种方式完成它,但是,对于我正在做的事情,我不想使用 AJAX。我想要一个服务器端解决方案。

我唯一的另一个想法是必须设置 $_SESSION 变量,然后在显示状态更新后以某种方式将其删除。

任何想法将不胜感激!

谢谢,

托尼

I am looking for a "neat" (clean) way to show a status message after submitting a form.

For example, if a user updates their profile, I want to show a DIV with the text "Your Profile Has Been Updated!".

Right now I am getting this done this way:

After posting the data and either inserting or updating the database table, I do a header("Location: updateprofile.php?status=updated");

This works just fine and dandy, however it isn't very clean. I don't want the query string.

Another method I tried was to set a variable of $submitted=1; and not do a redirect after. However, when a user is updating their information it still shows the old information in the form fields until a refresh. So this doesn't work either.

I know I could use AJAX to submit the form and accomplish it that way, however, for what I am doing I do not want to use AJAX. I want a server side solution.

My only other thought is having to set a $_SESSION variable then somehow remove it after showing the status update.

Any ideas would be appreciated!

Thanks,

Tony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

漆黑的白昼 2024-12-17 18:30:10

大多数时候,这是通过存储需要显示的任何闪存消息的会话变量来处理的。

在共享视图(布局、元素)中,将

<?php
    if (isset($_SESSION['flashMessage']) && !empty($_SESSION['flashMessage'])) {
        echo <<<EOF
        <div class="flashMessage">
            {$_SESSION['flashMessage']}
        </div>
EOF;
        unset($_SESSION['flashMessage']);
    }
?>

$_SESSION['flashMessage'] 设置为“您的个人资料已更新”。在处理表单提交的任何代码中。

Most of the time this is handled through session variables that store whatever flash messages need to be shown.

In a shared view (layout, element) put something like

<?php
    if (isset($_SESSION['flashMessage']) && !empty($_SESSION['flashMessage'])) {
        echo <<<EOF
        <div class="flashMessage">
            {$_SESSION['flashMessage']}
        </div>
EOF;
        unset($_SESSION['flashMessage']);
    }
?>

Set $_SESSION['flashMessage'] to "Your profile has been updated." in whatever code is handling your form submissions.

迷迭香的记忆 2024-12-17 18:30:10

将状态存储在会话数组变量中,然后只需执行以下操作:

<?php 

if ( !empty( $_SESSION['statuses'] ) ) {

  foreach ( $_SESSION['statuses'] as $status ) {

    // Wrap in tags and format as needed
    echo $status;  

  }

  $_SESSION['statuses'] = Array();

}

Store the statuses in a session array variable, then simply do:

<?php 

if ( !empty( $_SESSION['statuses'] ) ) {

  foreach ( $_SESSION['statuses'] as $status ) {

    // Wrap in tags and format as needed
    echo $status;  

  }

  $_SESSION['statuses'] = Array();

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