Re: using meta http-equiv in php
am 03.04.2008 13:13:07 von gordonOn Mar 9, 2:09=A0pm, Sudhakar
> i have a registration page which is a self submitting form
> action=3D"" method=3D"POST"
>
> id=3D"test2" name=3D"registrationform">
>
> where in a user fill a form, after the data has been inserted to a
> database i would like to redirect the user to a different
>
> page. i am not able to use header(Location: filename.html) as i have
> echo statements before and i get a message that headers
>
> have already been sent. so due to this i am using
>
> echo ("
> firstname=3D$firstname'> "); this works perfectly fine.
>
> however if a user disables meta refresh which is a very small
> possibility the above meta tag would not execute to avoid such
>
> a situation i would like to use echo statements to create a new page
> which would have html tags and display a similar page to
>
> thankyou.php
>
> in my case the php code is placed in the middle of the page which
> displays messages that a user did not enter in the form.
>
> the page is so structured that there is some information written using
> html tags followed by the registration questions where
>
> the php code is present to validate. i have used
>
> echo ("
=3D
> $fname'> ");
> redirectingthepage();
> exit;
>
> i have used the above code so that even if the echo ("
> equiv=3D'refresh' content=3D'0;url=3Dthankyou.php?fname=3D$fname'>
>
> "); is not executed the redirectingthepage() function will be
> executed.
>
> my question is due to the structure of the page whatever text is
> present before the form that text is appearing again
>
> followed by the text i have inside redirectingthepage() function, i do
> not want this to happen. i would like what is written
>
> in redirectingthepage() function only to appear. i have defined
> redirectingthepage() function in a separate file and i am
>
> calling the file which has redirectingthepage() function by using
> include statement in the registration page.
>
> please advice how i can display what is defined in
> redirectingthepage() function ONLY if the refresh is disabled by the
> user
>
> thanks.
Hi.
Headers, as their name suggests, must be sent before any other
output. There is no way around this, it's just a fact of how HTTP
works. First it sends the headers, then a blank line, then the
document content (the HTML). Any PHP code (or indeed HTML code
embedded in your PHP) that is sent to the browser triggers the sending
of the blank line, so it's too late to send any more headers after
this point.
The proper solution to this problem would be to refactor your code to
remove any output that occurs prior to the header being sent. The
pattern I typically use for what you're doing (a form that submits to
itself and redirects somewhere else on success) is as follows:
if ($_POST)
{
if (Your test for valid form data here)
{
if (Database update here)
{
header ('Location: http://your.location.here');
die ();
}
else
{
$errors =3D 'Could not submit to database';
}
}
else
{
$errors =3D 'Form data inorrect';
}
}
>?
if ($errors)
{
echo ('
}
?>