View Full Version : Page refresh
InNeedofHelp
06-08-2006, 10:33 PM
what's the simplest method of making the page refresh using PHP code?
echo a meta refresh? echo some javascript?
if neither of those is best, what is? and how would one script it?
also if it is the meta refresh, how does one script that?
thanks
djr33
06-08-2006, 10:45 PM
javascript is the worst... it is JS dependant and varies by browser. JS can also be turned off, so it's not reliable.
Use meta:
<meta http-equiv="refresh" content="5;url=newpage.php">
Must be in the head section.
Note that the 5 means five seconds... use whatever number of seconds you want, 0 for immediate.
no quote after url, any relative or absolute url will work.
Yeah, echo that: echo "...here...";
The best method is to send an http header with php. That will be even better than meta. Not sure about that code, though.
InNeedofHelp
06-08-2006, 11:38 PM
Allright, great. Thanks.
One more thing -
When the page does reload, are all of the $_POST variables cleared?
For example, i go from a post reply page, to my forums page. My forums page inputs the reply info using the $_Post variables, and then the page refreshes from a meta tag. Are those $_Post variables cleared on refresh?
djr33
06-09-2006, 12:31 AM
I understand the question, but I'm not quite sure.
Have you noticed that when you are browsing and have just sent a form, then hit refresh, that it says "Warning: the page you are refreshing contains POST data. Refreshing will send it again"; or something.
I believe it depends on the browser.
I'm not sure if it only refreshes with post data if you do so by hitting the refresh command, rather than, for example, a meta refresh.
However, with a meta refresh, you can specify the same URL as the current page, and it won't resend the data.
<meta http-equiv="refresh" content="0">
Where 0 is the time in seconds. Since the url is omitted, it's just the current page.
That WOULD likely resend the data.
With the example from above:
<meta http-equiv="refresh" content="5;url=newpage.php">
It would just be transferred to newpage.php, whether or not it was the current page, without any post data as the meta refresh is basically equivalent to an automatic link set to be "clicked" at X seconds into the page.
Bottom line-- you can certainly insure that the data WON'T be sent again if you specify the page as a url, rather than just refreshing the current page by omitting the url attribute, but I do not know if you can guarantee that the post data WILL be sent.
*Note: POST may be complex, but GET is very easy. If your forms use the get method, the data will be appended to the end of the URL.
Refreshing or visiting index.php?var=val WILL resend the get data;
refreshing or visiting index.php will NOT resend the get data.
The POST data is never resent.
djr33
06-09-2006, 06:46 AM
It definitely is if the user clicks refresh, at least in Safari and mozilla... not sure about IE.
Is this disabled, then, from automatic refreshes? Only works if it's user initiated?
It definitely is if the user clicks refresh, at least in Safari and mozilla... not sure about IE.No, the browser detects that the request required POST data and asks if the user wishes to resend it. If you use a meta refresh, the browser will simply navigate to the page to which you point it. There is no way to include POST data in the meta refresh.
InNeedofHelp
06-09-2006, 06:46 PM
That is good news. Thanks Twey and djr. :D
djr33
06-09-2006, 08:16 PM
Ok, that makes sense then.
I know I've asked this before, but I never really got an answer, Twey. Is there any way to send the post data without sending a form?
(I suppose one could code javascript to send a hidden form when a link is clicked... that might work)
That's actually the simplest and most effective way of doing it. For a bit more flexibility, there's always AJAX, of course.
djr33
06-10-2006, 04:34 AM
AJAX can send post data?
Yep.
After the open() call, rather than send(null) or send(''), you should send("name1=value1"); send("name2=value2"); and so on, for as many name/value pairs as you like.
djr33
06-10-2006, 06:35 PM
Is there any way to attach that to a link?
(I suppose you could add an onClick attribute)
mwinter
06-10-2006, 09:06 PM
[...] rather than send(null) or send(''), you should send("name1=value1"); send("name2=value2"); and so on, for as many name/value pairs as you like.I don't know if it was your intent, but that reads like a suggestion to call the send method twice or more for a single request.
To send data in a POST request, one has the option of either composing a query string-like set of data where name/value pairs are combined with an equals symbols (=), and pairs are separated by an ampersand (&). Alternatively, the entity body should be a well-formed MIME message body. In either case, both a Content-Type and Content-Length header should be included in the request headers unless there is very good reason not to include them (and there should never be, in this instance).
The HTML specification defines the unregistered media type, application/x-www-form-urlencoded for use when taking the first option, though other values are also known (and quite similar).
Is there any way to attach that to a link?Why on Earth bother? Just use a form and be done with it. If you really want the button to look like a link, style it as such.
Links are for navigation, forms are for user input. Use them both properly. Don't arse around with AJAX when something far more reliable and simple is available instead.
Mike
djr33
06-10-2006, 09:26 PM
POST data being sent by a form can be limited, so it's nice to have a couple ways around it.
I think the best idea is to use a form with a button styled as a link.... good idea.
Anyway.... I'm just curious, more than really needing a solution.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.