469,366 Members | 2,254 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Redirect after running PHP script

Ok this is the script that handles the form submission and input's it to the SQL database. It works 100%.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("*****","*****","*****");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7. mysql_select_db("ccsdesig_first", $con);
  8. $sql="INSERT INTO ***** (Name,Surname,Number,Email,MovingFrom,MovingTo,Date) 
  9.         VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
  10.  
  11. $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
  12.         VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
  13.  
  14. if (!mysql_query($sql,$con))
  15.   {
  16.   die('Error: ' . mysql_error());
  17.   }
  18. mysql_close($con)
  19.   echo "Done";
  20. ?>
  21.  
What I would now like the script to do is to send the user to another page - what do I use to automatically redirect?
I've tried:
Expand|Select|Wrap|Line Numbers
  1. header("Location:http://www.xxxxxxx.com/viewguestbook2.php");
  2.  
and
Expand|Select|Wrap|Line Numbers
  1. <META HTTP-EQUIV="refresh" content="0;URL=index.php">
  2.  
And neither works - could someone point me in the right direction please?

My thanks in anticipation
Apr 20 '10 #1
16 5915
chathura86
227 100+
you can use a JS to redirect user

Regards
Apr 20 '10 #2
Atli
5,058 Expert 4TB
Hey.

What happened when you tried the header() function? It should have worked.
(Try turning on the error messages to see what really happened.)

You can do three things to redirect a user:
  1. Send a Location header, using the header function. - This is the preferred method, as it is the "shortest" way to send the browser the request to relocate.
  2. Add a <meta> "refresh" header. - This is a close second, only because it requires the browser to parse the HTML before actually getting the request.
  3. Send a JavaScript "location.href" value. - This should be avoided, because JavaScript can not be counted on in all cases. Users can turn off JavaScript, and some browser (and I use that term loosely) don't support it.
Apr 20 '10 #3
somehow it posted twice
Apr 20 '10 #4
Hi,

I get the following error:
Parse error: syntax error, unexpected T_STRING in /home/****/public_html/insert.php on line 21
When using the following:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', true);
  4. $con = mysql_connect("localhost","*****","*****");
  5. if (!$con)
  6.   {
  7.   die('Could not connect: ' . mysql_error());
  8.   }
  9. mysql_select_db("*****", $con);
  10. $sql="INSERT INTO contactdetails (Name,Surname,Number,Email,MovingFrom,MovingTo,Date) 
  11.         VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
  12.  
  13. $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
  14.         VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
  15.  
  16. if (!mysql_query($sql,$con))
  17.   {
  18.   die('Error: ' . mysql_error());
  19.   }
  20. mysql_close($con)
  21. header("Location: http://www.xxx.com/email.php");
  22. exit;
  23. ?>
  24.  
Apr 20 '10 #5
Atli
5,058 Expert 4TB
You are missing the semi-colon (;) from line #20. All lines must end in a semi-colon.
Apr 20 '10 #6
@Atli
Warning: Cannot modify header information - headers already sent by (output started at /home/****/public_html/insert.php:1) in /home/****/public_html/insert.php on line 21
Apr 20 '10 #7
chathura86
227 100+
you cannot have any echo statements or none php codes before the header

ie. you cannot send any output to the browser before header()

make sure that (even a white space could case an error)

Regards
Apr 20 '10 #8
Thanks for your reply.
I checked through the code and dont think anything is posting to the browser at all. Perhaps I have missed something though?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', true);
  4. $con = mysql_connect("*","*","*");
  5. if (!$con)
  6.   {
  7.   die('Could not connect: ' . mysql_error());
  8.   }
  9. mysql_select_db("*", $con);
  10. $sql="INSERT INTO contactdetails (Name,Surname,Number,Email,MovingFrom,MovingTo,Date) 
  11.         VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
  12. $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
  13.         VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
  14. if (!mysql_query($sql,$con))
  15.   {
  16.   die('Error: ' . mysql_error());
  17.   }
  18. mysql_close($con);
  19. header("Location: http://www.*.co.za/email.php");
  20. exit;
  21. ?>
  22.  
Apr 20 '10 #9
Warning: Cannot modify header information - headers already sent by (output started at /home/*/public_html/insert.php:1) in /home/*/public_html/insert.php on line 19
Apr 20 '10 #10
Atli
5,058 Expert 4TB
If you are getting that error, something is being printed before the header() call.

This is usually caused by white-spaces or other junk before the <?php block, BOM chars in Unicode documents or random echos in the code. - Check the source in the browser. That should include the junk chars that are causing this. (Post it here if you want us to take a look at it.)

You can also read this article for more details about this error, and how to fix it.
Apr 20 '10 #11
I read the article - thank you.
As far as I can see I have removed all the white spaces. Below is the code - Pls can you check where I have gone wrong:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con = mysql_connect("*","*","*");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7. mysql_select_db("*", $con);
  8. $sql="INSERT INTO contactdetails (Name,Surname,Number,Email,MovingFrom,MovingTo,Date)
  9. VALUES ('$_POST[Name]','$_POST[Surname]','$_POST[Number]','$_POST[email]','$_POST[MovingFrom]','$_POST[MovingTo]','$_POST[Date]')";
  10. $sql="INSERT INTO destination (CurrentPremises,CurrentFloor,DestinationPremises,DestinationFloor,WrapEverything,Unpack,Hangerpack,SendMaterials,InsureEverything) 
  11. VALUES ('$_POST[CurrentPremises]','$_POST[CurrentFloor]','$_POST[DestinationPremises]','$_POST[CurrentFloor]','$_POST[WrapEverything]','$_POST[Unpack]','$_POST[Hangerpack]','$_POST[SendMaterials]','$_POST[InsureEverything]')";
  12. if (!mysql_query($sql,$con))
  13. {
  14. die('Error: ' . mysql_error());
  15. }
  16. mysql_close($con);
  17. header("Location: http://www.*.co.za/email.php");
  18. exit;
  19. ?>
  20.  
Apr 21 '10 #12
Atli
5,058 Expert 4TB
We aren't talking about white-spaces inside the <?php .. ?> blocks, but outside them. White-spaces inside them don't matter.

Expand|Select|Wrap|Line Numbers
  1.    <?php
  2. // There are three white-spaces before the <?php tag 
  3. // above this comment. This will cause the error.
  4.  
  5.    $variable = '
  6.         something
  7.         something else.';
  8. // The white-spaces in the above variable,
  9. // or the string, have no effect.
  10. ?>
Did you check out the source of the page in your browser? How did it look like?
Apr 21 '10 #13
Understood and as you can see there are no white spaces in the code I posted. I dont know how to view a PHP files source without it actually processing the script first. Once it processes the script the source is the following:
Expand|Select|Wrap|Line Numbers
  1. <br /> 
  2. <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/ccsdesig/public_html/insert.php:1) in <b>/home/ccsdesig/public_html/insert.php</b> on line <b>17</b><br /> 
  3.  
If I open the PHP with notepad the source looks exactly the same as it does in expression web and as I posted above.
Apr 21 '10 #14
Atli
5,058 Expert 4TB
Ok. The error still points to line #1 of the file, saying the output is starting there.

I would guess that your IDE is adding a BOM to your document; a Unicode special character that is meant to tell how the Unicode chars in the document are structured.

To check and fix that, you need an editor that can re-encode the file. I've used Notepad++ for that in the past. It's a very small and simple editor. A sort of advanced version of Notepad.

You can open your document in there, select "Encoding > Encode in UTF-8 without BOM", and then save the file.

That might fix the error.
Apr 21 '10 #15
The encoding resolved it!
Thank you!
Apr 21 '10 #16
Atli
5,058 Expert 4TB
No problem. Glad it worked out :)
Apr 21 '10 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Sentinel | last post: by
5 posts views Thread by Josselin | last post: by
4 posts views Thread by Dave | last post: by
1 post views Thread by gnawz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.