homepage Welcome to WebmasterWorld Guest from 54.167.185.110
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Refresh page
how to refresh page in php
copongcopong




msg:1285147
 12:44 am on Nov 20, 2002 (gmt 0)

Is there a way to put a header in a page using php so that it would refresh the page? is it the same as the no cache in header?

for example a create a form that is to be process by the same page and I would like to make sure that once the data was input from the form, the page itself would refresh to load again since the data would appear on the page and even save the data into a cookie.

Thanks.

 

amanda21




msg:1285148
 6:55 am on Nov 20, 2002 (gmt 0)

I'm pretty sure you cant refresh the page using php, but you could use php to write out a meta refresh tag or javascript refresh.

Basically put a hidden field in your form called 'submitted' and use an if statement that says, if submitted = yes then write the meta refresh/javascipt.

mack




msg:1285149
 7:05 am on Nov 20, 2002 (gmt 0)

Firstly in not a php expert...

would it not work to insert a refresh tag in the head almost as if it where an html document using the folowing page setup...

<html>
<head>
meta refresh tag
<title>title</title>
</head>
<body>
<?php
page content (script)
?>
</body>
</html>

amanda21




msg:1285150
 8:05 am on Nov 20, 2002 (gmt 0)

If you use meta refresh to refresh the current page you end up in a loop. That's why you need to check if the form has been submitted.

ie
<?
if ($HTTP_POST_VARS["submitted"] == "yes"){
echo "<meta http-equiv='refresh' content='0;URL=thepage.php'>";
}
?>
Then in the form:
<input type="hidden" name="submitted" value="yes">

That should do it, although I haven't tested it.

ps, please ignore any typos - I'm not good before 9am

copongcopong




msg:1285151
 10:07 pm on Nov 20, 2002 (gmt 0)

Thanks these are very helpful. Is there a way not to use the META REFRESH and JavaScript on this like using a referrer page that would direct back the information to that same page?

andreasfriedrich




msg:1285152
 10:40 pm on Nov 20, 2002 (gmt 0)

mack wrote
would it not work to insert a refresh tag in the head almost as if it where an html document

It is an html document that gets sent to the UA.

I donīt really understand why you want to refresh the document containing the form. If you refresh the page before the user submits the form you wonīt get any data and the user will be confused.

There is no need to refresh the page after it is submitted to display the data the user submitted again or save them in a cookie. Just send a cookie in the header of your HTTP reply to the userīs POSTed request. To display the data the user entered just send them in the html document contained in the body of that same reply.

Your form processing would then look like this.

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
// check supplied data
if (count($errors) > 0) {
// process data
setcookie('name', $data);
// echo the data user submitted
exit;
} else {
// clean up
}
}
// display form

Andreas

andreasfriedrich




msg:1285153
 3:18 pm on Nov 21, 2002 (gmt 0)

I guess it was a bit late when I wrote that code. It should really look like this:

setcookie('name', $data); 
header("Location: $new_url");
// echo the data the user submitted on $new_url
// when that gets requested
exit;

Andreas

copongcopong




msg:1285154
 11:29 pm on Nov 21, 2002 (gmt 0)

here is what I am really trying to do.

I created a form that would accept data for the font size; small, medium and large.

the form would be processed by the same page. and would send a data or create a cookie with the data of the font size.

then then same page would also use or get the data from the cookie to refresh the page and change it font size.

I have created one using meta refresh tag and it is working.

how about a sort or referrer page that would receive the data from the form the refer it back to the current page?

any input would be very much appreciated.

thanks. :)

copongcopong




msg:1285155
 12:54 am on Nov 22, 2002 (gmt 0)

Thanks andreas, I was slow and your steps that you corrected Do work for me too!

:)

andreasfriedrich




msg:1285156
 1:27 am on Nov 22, 2002 (gmt 0)

Glad you got it to work.

Andreas

Tapolyai




msg:1285157
 1:49 am on Nov 22, 2002 (gmt 0)

I often code my admin pages to do just what you described.

In essence:

//adminpage.php
...
<HEAD>
{
check for stuff if we need to mess up the font or something, spit CSS code accordingly
See if isset($flag) and other form fields are not empty($formfield) or strlen($otherformfield)<1 stuff!}
</HEAD>
...
switch ($flag)
{
case action1: { Do stuff & set flag if need to}
case action2: { Do stuff & set flag if need to}
case action2: { Do stuff & set flag if need to}
default : { Do stuff & set flag if need to (this is what we will do the first time, but not necessarily every time}
}
This is what we will do every time, irrelevant of the flag.
<form action="adminpage.php?flag=$appropriate_action" method="POST">
can set the flag here too by multiple
<input type=submit value=flag1 etc...> or
<input ... name=fontsize (we can use it in the header when submitted)></form>

The form would pass all the information that you need to itself. It will pass both the POST information from the <form> AND the GET information you faked in the <form> URL. Combining POST & fake-GET works perfectly in IE, Mozilla and Opera for me.

Make sure you do not run over the variable names between the POST and GET, i.e. name them the same. Also make sure you use the following format:

METHOD="http://www.x.net?field1=value1&field2=value2&field3=value3"

Notice how the first field1 is separated by a? (question mark), but all following fields are separated from each other by an & (ampersand).

Let me know if you need more detailed code.

andreasfriedrich




msg:1285158
 2:30 am on Nov 22, 2002 (gmt 0)

I must have been still tired when I corrected [webmasterworld.com] my first post [webmasterworld.com] above since there is still an error in it.

if (count($errors) > 0) {

should read:

if (count($errors) == 0) {

You would want to process the form data only if there were no errors in it ;)

Andreas

copongcopong




msg:1285159
 5:00 am on Nov 22, 2002 (gmt 0)

got another problem (again) ...

for example the page where you try to change the font size (from a form) is: www.site.com?id=2&p=1

how can i make it sure that when it refresh the page it would still refer to that address (www.site.com/test/test.php?id=2&p=1) and not on the mother page (www.site.com/test/test.php)?

in the headers part "url=", what i use is $php_self. anything I should change?

thanks for your input on this. they are all very helpful. :)

andreasfriedrich




msg:1285160
 5:32 am on Nov 22, 2002 (gmt 0)

Use $_SERVER['QUERY_STRING'] to add the query string as well.

Andreas

copongcopong




msg:1285161
 12:59 am on Nov 23, 2002 (gmt 0)

Thanks again andreas, ...

it tried that but i experience problem adding the $_SERVER['QUERY_STRING'] in the header() part with the $PHP_SELF.

anyway, the workaround i did was to put them (PHP_SELF and QUERY_STRING) in the a hidden input in the form, $_POST['hiddeninput'] and have it put into the header url and it is working.

my question is ....

is there any security issue when i doing that?

Thanks again.

copongcopong




msg:1285162
 1:02 am on Nov 28, 2002 (gmt 0)

I got this tip from w3 while validating my page:

[w3.org...]

anyway another lesson learned and i do the http redirect to refresh my page. :)

hope this helps.


Use standard redirects: don't break the back button!

Don't use "refresh" to redirect
If you want [example.org...] to actually display what's at [example.org...] do notuse "refresh" techniques like :

<META HTTP-EQUIV=REFRESH CONTENT="1; URL=http://machine/doc3.html">.Why? because it breaks the "back" button. If the user presses the "back" button, the refresh works, and the user bounces forward. The user will most likely get very annoyed, and close the window. That's not what you want, is it?


Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved