简体   繁体   English

提交表单后刷新页面

[英]Refresh page after form submitting

I have a little problem. 我有一点问题。 I want to reload my page after submitting a form. 提交表单后,我想重新加载页面。

<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>

only use 只用

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

right after insert query before } example 在}示例之前插入查询之后

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

on your full page, you could have this 在您的整个页面上,您可以拥有这个

<?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

?>

If you want the form to be submitted on the same page then remove the action from the form attributes. 如果要在同一页面上提交表单,请从表单属性中删除action

<form method="POST" name="myform">
 <!-- Your HTML code Here -->
</form>

However, If you want to reload the page or redirect the page after submitting the form from another file then you call this function in php and it will redirect the page in 0 seconds. 但是,如果要从其他文件提交表单后重新加载页面或重定向页面,则可以在php调用此函数,它将在0秒内重定向页面。 Also, You can use the header if you want to, just make sure you don't have any content before using the header 另外,您可以根据需要使用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")
   <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");
    }
?>

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

您可以使用:

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

You want a form that self submits? 您需要自我提交的表格吗? Then you just leave the "action" parameter blank. 然后,您只需将“ action”参数留空即可。

like: 喜欢:

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

If you want to process the form with this page, then make sure that you have some mechanism in the form or session data to test whether it was properly submitted and to ensure you're not trying to process the empty form. 如果要使用此页面处理表单,请确保表单或会话数据中具有某种机制来测试表单是否已正确提交,并确保您不尝试处理空表单。

You might want another mechanism to decide if the form was filled out and submitted but is invalid. 您可能希望使用其他机制来决定是否填写并提交了表单,但该表单无效。 I usually use a hidden input field that matches a session variable to decide whether the user has clicked submit or just loaded the page for the first time. 我通常使用与会话变量匹配的隐藏输入字段来决定用户是单击提交还是第一次加载页面。 By giving a unique value each time and setting the session data to the same value, you can also avoid duplicate submissions if the user clicks submit twice. 通过每次提供一个唯一值并将会话数据设置为相同的值,如果用户单击两次提交,则还可以避免重复提交。

  //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         
  ?>

Hope this helps. 希望这可以帮助。

LOL, I'm just wondering why no one had idea about the PHP header function : 大声笑,我只是想知道为什么没人对PHP标头功能有所了解:

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

I use this, so user is not prompt to resubmit data if he refresh the page. 我使用它,因此如果用户刷新页面,则不会提示用户重新提交数据。

See Refresh a page using PHP for more details 有关更多详细信息,请参见使用PHP刷新页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM