Reloading a page with parameters
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:
- 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.
So my ques开发者_StackOverflowtion is whether it is possible in Javascript to reload a page with a parameter? Or do you know of another solution?
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.
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.
In another topic i found that
window.location.replace(window.location.href)
will keep the parameters.
In PHP using the header function produces the infamous "headers already sent by" error.
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.
In Javascript, a location.reload does reload the page but, again, the parameter is missing leading to the same result.
Then pass it along? This should work, I think?
window.location( window.location.toString( ) );
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,
<?php
$id = $_GET['user_id'];
echo "<script type='text/js'>
document.location='".$_SERVER['PHP_SELF']."&id=".$id."';
</script>";
?>
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).
<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>
精彩评论