PHP:单击提交按钮后重定向同一页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17125688/
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
PHP: Redirect same page after click the submit button
提问by Philistyne Brigid Bellisima
Is there a way to redirects to the same page using PHP.??? I just need to reload or refresh the gridview. I don't need to redirect to a website or link, I need is redirect it to the same page or same form like main.php.
有没有办法使用 PHP 重定向到同一页面。???我只需要重新加载或刷新 gridview。我不需要重定向到网站或链接,我需要将它重定向到与 main.php 相同的页面或相同的表单。
I already used the header(), but its not working to me and all I see is linking in the website.
我已经使用了 header(),但它对我不起作用,我看到的只是网站中的链接。
Thanks.
谢谢。
采纳答案by tsknakamura
Here are two example to redirect to a form. Lets say that your filename is main.php
这是重定向到表单的两个示例。假设您的文件名是 main.php
<form action="main.php">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>
Or you can use this
或者你可以使用这个
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name">
<input type="submit" name="submit" value="Submit">
</form>
Did that answer your question?
那回答了你的问题吗?
回答by Gabriele Medeot
Simply do:
简单地做:
$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
回答by inf3rno
You should redirect with a location header after every post, because otherwise when the user presses the refresh button, it will send again the same form...
您应该在每次发布后使用位置标题重定向,否则当用户按下刷新按钮时,它会再次发送相同的表单...
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
file_put_contents('data.txt', $_POST['data']);
header('location: ' . $_SERVER['PHP_SELF']);
} else {
header('content-type: text/html; charset=utf-8');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="application/x-www-form-urlencoded; charset=utf-8">
<input name="data" type="text" value="<?php echo file_get_contents('data.txt'); ?>"/>
<button>küldés</button>
</form>
<?php
}
Btw. if you want to do proper work, you should try out a php framework instead of this kind of spaghetti code...
顺便提一句。如果你想做正确的工作,你应该尝试一个 php 框架而不是这种意大利面条式的代码......