php 表单提交后刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10643626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh page after form submitting
提问by Dário Viegas
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>
回答by Gul Nawaz
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'>";
}
回答by cantsay
<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
?>
回答by Othman
If you want the form to be submitted on the same page then remove the actionfrom 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 phpand it will redirect the page in 0 seconds. Also, You can use the headerif you want to, just make sure you don't have any content before using the header
但是,如果您想在从另一个文件提交表单后重新加载页面或重定向页面,那么您可以调用此函数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")
回答by Neku
<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");
}
?>
回答by Kristian
action attribute in <form method="post" action="action=""">should be just action=""
动作属性<form method="post" action="action=""">应该只是action=""
回答by SoEnLion
You can maybe use :
你也许可以使用:
<form method="post" action=" " onSubmit="window.location.reload()">
回答by George Garchagudashvili
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 PHPfor more details
有关详细信息,请参阅使用 PHP 刷新页面
回答by Kasapo
You want a form that self submits? Then you just leave the "action" parameter blank.
你想要一个自我提交的表格吗?然后您只需将“action”参数留空。
like:
喜欢:
<form method="post" action="" />
<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.
您可能需要另一种机制来确定表单是否已填写并提交但无效。我通常使用与会话变量匹配的隐藏输入字段来决定用户是单击提交还是第一次加载页面。通过每次赋予唯一值并将会话数据设置为相同值,如果用户单击提交两次,您还可以避免重复提交。
回答by Ranjan
//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.
希望这可以帮助。

