Page 1 of 1

a problem with refreshing the page

Posted: Tue Feb 20, 2007 11:56 am
by siyaco
hi to everybody
i wrote a simple code for a quest book. it shows the 8/1page messages and at the bottom a form to write the new message. the problem is this;
when somebody writes a new message and it appears on mysite.com/page.php?page=1
and then if the user wants to refresh the page this warning appears;
confirm
the page you are trying to view contains POST DATA. if you resend the data any action the form carried out will be repeated
i controlled it by a if condition not to write the same data. but still this warning appears.

how can i control these not to resend the same data when it is refreshed. for example i see that non forums behaves like this. you submit your message, and refresh several times but there is no warning or problem.

thanks all in advance

Posted: Tue Feb 20, 2007 11:59 am
by Burrito
user header() to redirect them to the page after the post completes

Posted: Tue Feb 20, 2007 12:05 pm
by siyaco
Burrito wrote:user header() to redirect them to the page after the post completes
thanks, i will look now. i hope i will understand :)

Posted: Tue Feb 20, 2007 12:19 pm
by siyaco
i am sorry but i did not understand well :oops: , may be because of my poor english. if you explain it for me i will be happy. a small reminder;

the user writes a new message and it appears on .mysite.com/mes.php?page=1
the user refreshes a warning appears. i want this;
when user refreshes none warning appears
thank again

Posted: Tue Feb 20, 2007 12:28 pm
by Burrito
after they post their message do this:

Code: Select all

// deal with posting stuff
mysql_query("INSERT INTO `table` ...");
header("Location: mes.php?page=1");

Posted: Tue Feb 20, 2007 12:42 pm
by siyaco
i tried it in localhost
for the first page it is shown on ..index1.php
the code is
if($user!=NULL && $message!=NULL)
{

$query = "INSERT INTO forum1 VALUES('', '".$user."', '".$date."', '".$time."', '".$message."')";
$result1 = mysql_query($query) or die('yazida bir hata oldu, ayni mesaji tekrarlamis olabilirsiniz');
header("Location: index1.php");
}
but it gives this error when the message is submitted
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\projeler\ziyaretci defteri\index1.php:11) in C:\wamp\www\projeler\ziyaretci defteri\index1.php on line 60
thanks

Posted: Tue Feb 20, 2007 1:53 pm
by Burrito
you can't output anything (even whitespace) above your call to the header() function.

Posted: Tue Feb 20, 2007 4:15 pm
by feyd
..and use a full url, no half-assing :P

Posted: Tue Feb 20, 2007 11:55 pm
by siyaco
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="Burrito"]you can't output anything (even whitespace) above your call to the [url=http://php.net/function.header]header()[/url] function.[/quote]

may be, i am wasting your time but i did not understand really 

[quote]..and use a full url, no half-assing [/quote]

i did it like this;

Code: Select all

if($user!=NULL && $message!=NULL)
    {
    
   $query = "INSERT INTO forum1 VALUES('', '".$user."', '".$date."', '".$time."', '".$message."')";
   $result1 = mysql_query($query) or die('there is a fault');
     header("Location: http://localhost/projeler/ziyaretci_defteri/index1.php");
    }
but still gives this error
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\projeler\ziyaretci_defteri\index1.php:11) in C:\wamp\www\projeler\ziyaretci_defteri\index1.php on line 62
thank all in advance


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Feb 21, 2007 12:18 am
by Kieran Huggins
Burrito wrote:you can't output anything (even whitespace) above your call to the header() function.

Posted: Thu Feb 22, 2007 1:59 am
by siyaco
i hope i don't waste your time but still i have not solved my problem. i read the php.net and i saw a warning below;
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
as i understand; if there is a form to send an output to the page, the header function gives error. so, it gives when i browse. and i put it in the code, which a form exists. is it the problem, did i understand correct and how can i solve my problem?

thanks all in advance

Posted: Thu Feb 22, 2007 2:11 am
by mikeq
It means you cannot output anything at all to the browser before calling that header function

this will fail (text is being output to the browser by the PHP print statement, before calling header())

Code: Select all

<?php
  print "some text going to the browser";
  header("location: index.php");
?>
this will fail (html tags have already been sent to the browser, before calling header())

Code: Select all

<html>
<head>
<?php
  header("location: index.php);
?>
</head>
<body>
</body>
</html>
this will fail

Code: Select all

                <------- there is a blank line at the very top of the script, this outputs to the browser, so headers 
                            already sent
<?php
  header("location: index.php);
?>
this is okay

Code: Select all

<?php
  $Query = "INSERT INTO blah blah";
  $Result = mysql_query($Query);

  //do other processing that does not involve writing output to the browser

  header("location: index.php");
?>
So in short no text whatsoever can be sent to the browser before calling the header("location: index.php").

Posted: Thu Feb 22, 2007 2:22 am
by siyaco
thanks for your explanations with examples.
so, i can't put header() into my code, because there are html tags, and other output keys like echo .
i think i have to find a new solution which i write about it above

thanks all in advance

Posted: Thu Feb 22, 2007 2:46 am
by feyd
Adjust your code such that all the logic is processed before presentation specific code needs to be executed.