javascript 使用参数重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6264037/
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
Reloading a page with parameters
提问by James P.
I have a page showing a random picture on each reload. This page only displays if a number is passed through $_GET (represents a user profile). To go to the next random picture I'd like a submit button to simply reload the page. However, I'm hitting two snags:
我有一个页面在每次重新加载时显示随机图片。此页面仅在通过 $_GET 传递数字(代表用户配置文件)时显示。要转到下一张随机图片,我想要一个提交按钮来简单地重新加载页面。但是,我遇到了两个障碍:
- In PHP using the
header
function produces the infamous "headers already sent by" error. Plus the user is redirected because no parameter is passed. - In Javascript, a
location.reload
does reload the page but, again, the parameter is missing leading to the same result.
- 在 PHP 中使用该
header
函数会产生臭名昭著的“headers already sent by”错误。另外,用户被重定向,因为没有传递参数。 - 在 Javascript 中, a
location.reload
确实会重新加载页面,但同样缺少参数导致相同的结果。
So my question is whether it is possible in Javascript to reload a page with a parameter? Or do you know of another solution?
所以我的问题是是否可以在 Javascript 中重新加载带有参数的页面?或者你知道另一种解决方案吗?
Edit: Did some tests with an alert box and the url shows up as it appears in the adress bar. No idea why the parameter isn't being passed.
编辑:使用警告框进行了一些测试,并且 url 显示在地址栏中。不知道为什么没有传递参数。
回答by Wesley van Opdorp
You should wonder why you receive the headers already sent
error. It is usually because your flow is incorrect. Make sure you handle your PHP
before you generate the output. Check for the submitted form at the top of your code - and safely redirect without executing anything else. Should solve your problem.
您应该想知道为什么会收到headers already sent
错误消息。这通常是因为您的流程不正确。确保PHP
在生成输出之前处理您的。检查代码顶部提交的表单 - 并安全地重定向而不执行任何其他操作。应该可以解决你的问题。
回答by joakimdahlstrom
In another topic i found that
在另一个主题中,我发现
window.location.replace(window.location.href)
window.location.replace(window.location.href)
will keep the parameters.
将保留参数。
回答by joakimdahlstrom
Instead of location.reload
the page, you can use document.location
to redirect the page to the same page with a parameter passed through GET. For example,
location.reload
您可以使用document.location
通过 GET 传递的参数将页面重定向到同一页面,而不是页面。例如,
<?php
$id = $_GET['user_id'];
echo "<script type='text/js'>
document.location='".$_SERVER['PHP_SELF']."&id=".$id."';
</script>";
?>
回答by Berry Langerak
In PHP using the header function produces the infamous "headers already sent by" error.
在 PHP 中,使用 header 函数会产生臭名昭著的“headers already sent by”错误。
That's simple enough: move the code that does the header( ) call up to the top (before any other output). "Headers already sent" is infamous, but also incredibly easy to solve.
这很简单:将调用 header() 的代码移到顶部(在任何其他输出之前)。“标题已经发送”是臭名昭著的,但也非常容易解决。
In Javascript, a location.reload does reload the page but, again, the parameter is missing leading to the same result.
在 Javascript 中, location.reload 确实会重新加载页面,但同样,缺少参数导致相同的结果。
Then pass it along? This should work, I think?
然后传过去?这应该有效,我想?
window.location( window.location.toString( ) );
回答by James P.
Ok, I might as well post this as it's been a pain getting something working through javascript and PHP and I don't have enough time to get something in ajax working. A simple workaround is to simply specify the parameter in the action attribute of the form (special markup is from smarty).
好吧,我不妨发布这个,因为通过 javascript 和 PHP 获得一些东西很痛苦,而且我没有足够的时间在 ajax 中获得一些东西。一个简单的解决方法是在表单的 action 属性中简单地指定参数(特殊标记来自 smarty)。
<form Method="POST" ACTION="my_page.php?param={$smarty.get.i}">
<!-- PHP: If ( isset($_POST['next']) ) do SQL ... -->
<input type="submit" name="like" value="I like">
<input type="submit" name="next" value="Next" >
</form>