提交表单后刷新页面

25

我有一个小问题。我想在提交表单后重新加载我的页面。

<form method="post" action="">
   <textarea cols="30" rows="4" name="update" id="update" maxlength="200" ></textarea>
   <br />
   <input type="submit"  value=" Update "  id="update_button"  class="update_button"/>
</form>

2
你的目标是什么? - Shyju
这不是默认行为吗?你为什么在你的操作中放置了那个值?我认为这是错误的。 - davidbuzatto
我的目标是,在我提交后,有些东西会刷新我当前所在的页面。 - Dário Viegas
@DárioViegas 如果 """ 是一个错误,应该改成 "",那么你的问题现在解决了吗? - nl-x
我有一个答案,我想!你解决了你的问题吗? - Ilia
9个回答

73

仅使用

 echo "<meta http-equiv='refresh' content='0'>";

在}之前的插入查询示例

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

很好,这真的帮了我很多! - Tomm
这是一个真正的“刷新”,因此回答了问题,而许多其他答案则需要创建一个新的URL作为$location。对于URL中有数十个变量的人来说,这可能是耗时和繁琐的。 - user2954658
1
嘿,这对我有用,非常好。但是,它现在会刷新两次,一次来自表单,这个刷新并不真正起作用。另一个来自回显代码。你能停止第一个刷新吗? - Zack Cheang Weng Seong

11
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <!-- notice the updated action -->
<textarea cols="30" rows="4" name="update" id="update" maxlength="200" ></textarea>
<br />
<input name="submit_button" type="submit"  value=" Update "  id="update_button"  class="update_button"/> <!-- notice added name="" -->
</form>

在您的整个页面上,您可以拥有这个

<?php

// check if the form was submitted
if ($_POST['submit_button']) {
    // this means the submit button was clicked, and the form has refreshed the page
    // to access the content in text area, you would do this
    $a = $_POST['update'];

    // now $a contains the data from the textarea, so you can do whatever with it
    // this will echo the data on the page
    echo $a;
}
else {
    // form not submitted, so show the form

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <!-- notice the updated action -->
<textarea cols="30" rows="4" name="update" id="update" maxlength="200" ></textarea>
<br />
<input name="submit_button" type="submit"  value=" Update "  id="update_button"  class="update_button"/> <!-- notice added name="" -->
</form>

<?php

} // end "else" loop

?>

7
如果您希望表单在同一页上提交,则需要从表单属性中删除action
<form method="POST" name="myform">
 <!-- Your HTML code Here -->
</form>

然而,如果你想要在提交表单后从另一个文件重新加载页面或重定向页面,那么你可以在php中调用此函数,它将在0秒内重定向页面。同时,如果你想要使用header,请确保在使用header之前没有任何内容。

 function page_redirect($location)
 {
   echo '<META HTTP-EQUIV="Refresh" Content="0; URL='.$location.'">';
   exit; 
 }

 // I want the page to go to google.
 // page_redirect("http://www.google.com")

如果您想在单击提交后刷新页面,请使用第一个HTML代码。如果您想在单击提交后更改页面,则使用第二个代码。这是您的选择。 - Othman

6

我只是想知道为什么没有人了解 PHP header 函数

header("Refresh: 0"); // here 0 is in seconds

我使用这个功能,这样用户在刷新页面时就不需要重新提交数据。
更多细节请参见使用 PHP 刷新页面

2
这对我来说是最好的,是的。 - Mohamed Slimane

4

您可以尝试使用:

<form method="post" action=" " onSubmit="window.location.reload()">

3
请查看此解决方案:https://dev59.com/mpDea4cB1Zd3GeqPfLJo#42817540 - adilbo

3
   <form method="post" action="">
   <table>
   <tr><td><input name="Submit" type="submit" value="refresh"></td></tr>
   </table>
   </form>

<?php
    if(isset($_POST['Submit']))
    {
    header("Location: http://yourpagehere.com");
    }
?>

1

<form method="post" action="action=""">中的action属性应该只是action=""


0
  //insert this php code, at the end after your closing html tag. 

  <?php
  //setting connection to database
  $con = mysqli_connect("localhost","your-username","your-
  passowrd","your-dbname");

  if(isset($_POST['submit_button'])){

     $txt_area = $_POST['update']; 

       $Our_query= "INSERT INTO your-table-name (field1name, field2name) 
                  VALUES ('abc','def')"; // values should match data 
                 //  type to field names

        $insert_query = mysqli_query($con, $Our_query);

        if($insert_query){
            echo "<script>window.open('form.php','_self') </script>"; 
             // supposing form.php is where you have created this form

          }



   } //if statement close         
  ?>

希望这能有所帮助。

0

您想要一个可以自动提交的表单吗?那么,您只需要将 "action" 参数留空即可。

像这样:

<form method="post" action="" />

如果您想要使用此页面处理表单,请确保您在表单或会话数据中具有某些机制来测试它是否已经被正确提交,并确保您不会尝试处理空白表单。

您可能希望再使用另一种机制来判断表单是否已填写并已提交但无效。我通常使用与会话变量匹配的隐藏输入字段来决定用户是单击提交按钮还是第一次加载页面。通过每次都提供唯一的值并将会话数据设置为相同的值,您还可以避免重复提交,即使用户单击了两次提交按钮。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接