tags:

views:

29

answers:

3

Possible Duplicate:
Cannot modify header information - headers already sent, Why its happening

Hi I am new with php and I was hoping you could help. I hav a function that uses the javascript alert:

Function promptuser($msg)
{

 echo "<script type='text/

javascript'>

alert('$msg');

</script>";

}

Now I have saved this function under a file named 'prompt.php'.

Now in my main page:

Require ("prompt.php");

promptuser("you are in");

Header("location: index.php");

Exit;

The error i get:

"cannot modify header information - headers already sent...". 

Please help Thanx

A: 

you're echoing text, before sending header. That's forbidden, headers always should be the first thing to be sent to the client. I guess, you need to change the design of your application.

SilentGhost
+1  A: 

well, the problem is pretty much described in the error message.

To give you some background information: The HTTP Protocol defines the response of a request as a bunch of Key: Value pairs (called the header) and the body (which has no format). The header() function sends such a key: value pair, but you cannot do this, when you have already started sending the body (your echo).

Further, you should know that browsers normally don't show any body, when a Location header is present. So there is no real use of echoing the prompt.

ZeissS
A: 

First, change echo " alert('$msg'); "; to echo " <script>alert('$msg');</script> ";

Second, you can't send headers after you output something, in your case the echo.

You can, however, instead of sending the header, do this:

echo " <script>window.location = "MyURL";</script> ";

Ben