表单提交后刷新页面

时间:2012-05-17 21:13:12

标签: php html

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

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

9 个答案:

答案 0 :(得分:41)

仅使用

 echo "<meta http-equiv='refresh' content='0'>";
插入查询之前的

之前} 示例

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

答案 1 :(得分: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

?>

答案 2 :(得分:5)

如果您希望在同一页面上提交表单,请从表单属性中删除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")

答案 3 :(得分:2)

   <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");
    }
?>

答案 4 :(得分:1)

<form method="post" action="action=""">中的

操作属性应该只是action=""

答案 5 :(得分:1)

您可以使用:

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

答案 6 :(得分:0)

您想要一个自我提交的表单吗?然后你只需将“action”参数留空。

像:

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

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

您可能需要其他机制来确定表单是否已填写并已提交但无效。我通常使用与会话变量匹配的隐藏输入字段来决定用户是否单击了提交或者是第一次加载页面。通过每次提供唯一值并将会话数据设置为相同的值,如果用户单击提交两次,您还可以避免重复提交。

答案 7 :(得分: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         
  ?>

希望这有帮助。

答案 8 :(得分:0)

大声笑,我只是想知道为什么没人知道PHP header function

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

我使用它,因此如果用户刷新页面,则不会提示用户重新提交数据。

有关更多详细信息,请参见Refresh a page using PHP

相关问题