簡體   English   中英

如何在表單提交后在php中重新加載頁面

[英]How to reload a page in php after form submit

單擊“生成發票”按鈕時,將在新窗口中打開發票。

下面是示例代碼。 例如

    <form name="invoice" action="inv_rec.php" method="post" id="inv" target="invoices" onsubmit="return check_counter();" >
    <table>
    <tr>
       <td>
          <label for="cusname"><strong>Customer Name*&nbsp;</strong></label>
          <input type="text" size="20" name ="cusname" value="" id="Customername"     required/>
       </td>
       <td>
           <label for="for_place"><strong>Place</strong></label>
           <input type="text" size="20" name ="for_place" value=""  id="for_place" />
       </td>
    </tr>
    ........
    <tr>
        <td>
            <input type="submit" value="Generate Invoice" name="submit" id="sub">
        </td>
     </tr>
  </table> 
</form>

<script>
var counter = 1;
function check_counter()
{
 if(counter == 1)
{
 alert("Please enter the product details");
 return false;
}
 window.open('', 'invoices', 'width=650,height=800,status=yes,resizable=yes,scrollbars=yes');

return true;
}
</script>

在我的例子中,頁面將被重定向到inv_rec.php(在新窗口中打開),其中包含從mysql獲取的動態生成的數據,用戶需要在其中打印它。 我想清除在上一個窗口中打開的所有表單數據(顯示發票表單,即用戶填寫數據以生成發票)。

提交表單后重定向頁面。

header('location:redirect.php');

或者取消設置表格中的變量

在Jquery:

$('#form_id')[0].reset();

您可以使用javascript或jquery來實現此目的

在javascript中

document.getElementByName("invoice").reset();

在jquery

$("#formId").reset();

在表單中添加id以使用它

<form name="invoice" id="invoice" action="generate_invoice.php" method="post" target="_blank">

由於有人可能只是禁用javascript並仍然發布你的表單,我認為依靠JS為你做清潔是不安全的。

我建議查看post / redirect / get模式

你的問題不太清楚? 你是否正在清除表單字段,以便為另一組新輸入做好准備? 如果是這樣,你可以在調用java腳本中的window.open()方法之前調用reset。 這樣的事情:

<script>
var counter = 1;
function check_counter()
{
 if(counter == 1)
   {
      alert("Please enter the product details");
       return false;
    }
  $('#formid).reset();  //assuming you are using jquery
 window.open('', 'invoices','width=650,height=800,status=yes,resizable=yes,scrollbars=yes');

return true;
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM