【问题标题】:Refresh page after form submitting表单提交后刷新页面
【发布时间】:2012-05-25 11:55:00
【问题描述】:

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

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

【问题讨论】:

  • 你的目标是什么?
  • 这是默认行为,不是吗?你为什么把这个价值放在你的行动中?我认为这是错误的。
  • 我的目标是当我提交之后,我在那个时刻刷新页面的东西
  • @DárioViegas 如果 """ 是一个错误并且应该是 "" ,那么您的问题现在解决了吗?
  • 我想我有一个答案!你的问题解决了吗?

标签: php html


【解决方案1】:

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

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

我使用这个,所以如果他刷新页面,用户不会被提示重新提交数据。

详情请见Refresh a page using PHP

【讨论】:

  • 它对我来说是最好的,是的
【解决方案2】:
  //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         
  ?>

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    只使用

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

    插入查询之后 } 例子

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

    【讨论】:

    • 太好了,这真的帮助了我!
    • 这是一个真正的“刷新”,因此可以回答这个问题,而许多其他答案需要创建一个新的 URL 作为 $location。对于 URL 中有几十个变量的人来说,这可能既费时又烦人,等等。
    • 嘿,这对我有用,非常好。但是,它现在刷新两次,一次是从表单刷新,但实际上并不能正常工作。另一个来自回显代码。您是否设法阻止第一个刷新?
    【解决方案4】:

    如果您希望在同一页面上提交表单,请从表单属性中删除 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中的第一个,以防您希望在单击提交后刷新页面。如果您想在单击提交后更改页面,请使用第二个。这是你的选择。
    【解决方案5】:
    <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
    
    ?>
    

    【讨论】:

      【解决方案6】:
         <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");
          }
      ?>
      

      【讨论】:

        【解决方案7】:

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

        喜欢:

        &lt;form method="post" action="" /&gt;

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

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

        【讨论】:

          【解决方案8】:

          你可以使用:

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

          【讨论】:

          【解决方案9】:

          &lt;form method="post" action="action="""&gt; 中的 action 属性应该只是 action=""

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-09-21
            • 2013-02-09
            • 2016-09-19
            • 2011-01-27
            相关资源
            最近更新 更多