php 提交表单后如何自动刷新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30885877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:07:27  来源:igfitidea点击:

How to automatically refresh the page after submitting a form

phphtmlforms

提问by Prth

I have an HTML form, where, when I click "update" the changes do not reflect and it requires a refresh to see the changes. But when I reload, there's always Confirm Form Resubmission.

我有一个 HTML 表单,当我单击“更新”时,更改没有反映,需要刷新才能看到更改。但是当我重新加载时,总是有Confirm Form Resubmission.

I used <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">which means it submits on the same page.

我使用<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">这意味着它在同一页面上提交。

回答by Bruce

You can refresh the page in multiple way :

您可以通过多种方式刷新页面:

Way : 1

方式:1

if(isset($_POST[submits])) {
    header("location:index.php"); // your current page
}

Way : 2

方式 : 2

if(isset($_POST['submit'])) {
    //Your SQL Query
    echo "<meta http-equiv='refresh' content='0'>";
}

Way : 3(not recommended)

方式:3(不推荐)

onsubmit="window.location.reload();"it seems this does not work. So i use the timeout function in it. To make it work.

onsubmit="window.location.reload();"这似乎不起作用。所以我在其中使用了超时功能。让它发挥作用。

onsubmit="setTimeout(function() { window.location.reload(); }, 5)"

Way : 4

方式 : 4

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

回答by RuubW

I tend to use AJAX form submissions:

我倾向于使用 AJAX 表单提交:

// Bind the submit action
$('#myForm').submit(mySubmitFunction);

// Submit handler
function mySubmitFunction(e) {
    e.preventDefault();

    var form = $(this);

    $.ajax({
            url: form.attr('action'),
            data: form.serialize()
    }).done(function(xhr) {
            // My endpoint returns JSON output
            var responseJSON = false;
            try {
                    responseJSON = $.parseJSON(xhr);
            } catch (error) {}

            if (responseJSON && responseJSON.hasOwnProperty('result') &&
                responseJSON.result) {
                    // The important bit
                    location.reload();
            } else {
                    // Handle error
            }
    });
}

I chose for this in order to be able to provide quick feedback to the user. If you don't want to use AJAX, a simple header('Location: yourPage.php') in the PHP file processing your form will suffice.

我选择这样做是为了能够向用户提供快速反馈。如果您不想使用 AJAX,处理表单的 PHP 文件中的一个简单的 header('Location: yourPage.php') 就足够了。

回答by Gvidas

Your page already reloads after "update", because Confirm Form Resubmissionalert pops. I guess the problem is that in php script you display data before processing new Post data.

“更新”后您的页面已经重新加载,因为Confirm Form Resubmission警报弹出。我猜问题是在 php 脚本中你在处理新的 Post 数据之前显示数据。

回答by danielson317

So the main issue with what you are describing is that the url request method is POST. If you refresh the page after submitting the form the browser will attempt to POST again, just as if you hit the submit button.

因此,您所描述的主要问题是 url 请求方法是 POST。如果您在提交表单后刷新页面,浏览器将再次尝试 POST,就像您点击提交按钮一样。

This is actually a very natural way to submit data (to the url the form is generated on). Most open source CMSs will do the same thing and have the same behavior (wordpress, drupal).

这实际上是一种非常自然的提交数据的方式(到生成表单的 url)。大多数开源 CMS 会做同样的事情并具有相同的行为(wordpress、drupal)。

I built a custom system that did the same thing but had some complaints about refresh triggering the "Are you sure you want to submit this form again" instead of just reloading the form. So I solve the issue with @Bruce s solution. At the bottom of my page before I output anything I do a redirect to the current url. This changes the previous page in the browsers eyes to a GET instead of the POST and prevents the re-submission warning.

我构建了一个自定义系统,它执行相同的操作,但对刷新触发“您确定要再次提交此表单吗”而不只是重新加载表单有一些抱怨。所以我用@Bruce 的解决方案解决了这个问题。在我输出任何内容之前,在页面底部我重定向到当前 url。这会将浏览器中的上一页更改为 GET 而不是 POST 并防止重新提交警告。

As for your data not updating you are probably building your form before processing the post. Make sure processing the post is the first operation you perform.

至于您的数据未更新,您可能正在处理帖子之前构建表单。确保处理帖子是您执行的第一个操作。

My system is very different than this example code so don't expect this to work out of the box. But it should give you the right idea.

我的系统与这个示例代码非常不同,所以不要指望它开箱即用。但它应该给你正确的想法。

<?php
if (isset($_SERVER["REQUEST_METHOD"]) && ($_SERVER["REQUEST_METHOD"] == "POST"))
{
  // Validate my form.
  if ($valid)
  { 
    // Save data on successful validation.

    // Redirect to prevent refresh from triggering another
    // form submission. Optional but helpful.
    redirect($_SERVER['REQUEST_URI']);
  }
  else
  {
    // Not valid no harm in just reloading as the user will be
    // fixing their errors and resubmitting anyway.

    // Generate error messages, etc.
  }
}

// Retrieve your form values here after you have fully processed the POST
// operation.
$values = getDBValues();    

function redirect($path)
{
   // The path should be validated somehow as the user
   // could alter parts of it such as query parameters.
   // For simplicity I'm leaving that part out.
   header('Location: ' . $path);
   die();
}

?>
<html>
<form>
...