raydona Posted April 28, 2009 Share Posted April 28, 2009 Hi, How can I jump (redirect) to another page after say 6 seconds from inside PHP code. For example, I wish to say: $sql = "some code"; if(mysql_query($sql)) { echo "Successful<br/>"; //redirect to page A after 6 seconds } else { echo "Unsuccessful"; //redirect to page B after 6 seconds } The only way I know how to redirect to another page after some time is using javascript. <script type="text/javascript"> function timedMsg() { var t = setTimeout("location.href='file.html'",6000); } </script> But how can I write javascript inside PHP code? I would be very grateful for all help. Quote Link to comment https://forums.phpfreaks.com/topic/155926-calling-javascript-from-inside-php/ Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 $sql = "some code"; if(mysql_query($sql)) { echo "Successful<br/>"; $page = "pagea.html"; } else { echo "Unsuccessful"; $page = "pageb.html"; } ?> <script type="text/javascript"> function timedMsg() { var t = setTimeout("location.href='<?php echo $page; ?>'",6000); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/155926-calling-javascript-from-inside-php/#findComment-820796 Share on other sites More sharing options...
Prismatic Posted April 28, 2009 Share Posted April 28, 2009 Hi, How can I jump (redirect) to another page after say 6 seconds from inside PHP code. For example, I wish to say: $sql = "some code"; if(mysql_query($sql)) { echo "Successful<br/>"; //redirect to page A after 6 seconds } else { echo "Unsuccessful"; //redirect to page B after 6 seconds } The only way I know how to redirect to another page after some time is using javascript. <script type="text/javascript"> function timedMsg() { var t = setTimeout("location.href='file.html'",6000); } </script> But how can I write javascript inside PHP code? I would be very grateful for all help. <?php $sql = "some code"; if(mysql_query($sql)) { echo "Successful<br/>"; sleep(6); //6 seconds header("Location: newPage.php"); } else { echo "Unsuccessful"; sleep(6); //6 seconds header("Location: newPage.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155926-calling-javascript-from-inside-php/#findComment-820797 Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 <?php $sql = "some code"; if(mysql_query($sql)) { echo "Successful<br/>"; sleep(6); //6 seconds header("Location: newPage.php"); } else { echo "Unsuccessful"; sleep(6); //6 seconds header("Location: newPage.php"); } ?> Will not work. Output has been sent to the browser Alternatively he could use a META tag instead of Javascript, but yea. Quote Link to comment https://forums.phpfreaks.com/topic/155926-calling-javascript-from-inside-php/#findComment-820801 Share on other sites More sharing options...
Prismatic Posted April 28, 2009 Share Posted April 28, 2009 <?php $sql = "some code"; if(mysql_query($sql)) { echo "Successful<br/>"; sleep(6); //6 seconds header("Location: newPage.php"); } else { echo "Unsuccessful"; sleep(6); //6 seconds header("Location: newPage.php"); } ?> Will not work. Output has been sent to the browser Alternatively he could use a META tag instead of Javascript, but yea. Doh, good catch. Quote Link to comment https://forums.phpfreaks.com/topic/155926-calling-javascript-from-inside-php/#findComment-820806 Share on other sites More sharing options...
greghaynes Posted April 28, 2009 Share Posted April 28, 2009 This code works for me. I use it for all my redirects. <?php $location = 'http://www.someaddress.com'; header("Refresh: 5; URL=\"$location\""); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>Successful upload</title> </head> <body> <div id="Upload"> <h1>Successful File Upload</h1> <p class="centeralign">Congratulations! Your file upload was successful</p> <p class="centeralign">You will be redirected in a few seconds...if not click <a href="http://www.someaddress.com">here</a></p> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/155926-calling-javascript-from-inside-php/#findComment-820819 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.