Shiny_Charizard Posted March 5, 2008 Share Posted March 5, 2008 Is there any way I can redirect the user to another page by using the the header() function and still connecting to my MySQL Database? If I try redirecting the user after connecting to my database the page gives me an error. Can any one please help me? Here is the code: <?php #Include Top File require_once('top.php'); #Include Connect File require_once('connect.php'); #Display Page Heading echo "<center><b>Battle</b></center> <br /> <br />"; #If The User Has Submitted The Form if($_POST[submit]) { #If The Opponent ID Field Is Not Empty if(!empty($_POST[opponent])) { #Update The Mysql Table mysql_query("UPDATE members SET In_Battle?='Yes',opponent_id='$_POST[opponent]' WHERE user_id='$_SESSION[user_id]'"); #Redirects The User To The Battle File header("Location: battle.php"); /*echo "<script type='text/javascript'> window.location.href = 'battle.php' ; </script>";*/ } #If The Opponent ID Field Is Empty else { #Display Message echo "Please choose a user to battle! <br /> <br />"; } } #If The User Is Logged In if(!empty($username)) { #Display The Form echo "<form action='' method='POST' /> <br /> <font size='1'>Type the <u>ID</u> of the user you want to battle not the username.</font> <br /> <br /> <b>Opponet's ID</b> <br /> <br /> <table rules='rows'><tr><td class='headings'> <input type='text' name='opponent' /> </td></tr></table> <br /> <br /> <table rules='rows'><tr><td class='headings'> <input type='submit' name='submit' value='Battle' /> </td></tr></table> </form>"; } #If The User Is Not Logged In else { #Display Message echo "<center> Sorry, you must be logged in to view this page. <br /> <a href='login.php'>Login?</a> </center>"; } echo "<br /> <br /> </td>"; #Include The Bottom File require_once('bottom.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/ Share on other sites More sharing options...
roopurt18 Posted March 5, 2008 Share Posted March 5, 2008 http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-483400 Share on other sites More sharing options...
trq Posted March 5, 2008 Share Posted March 5, 2008 It gives you an error because your outputting html prior to calling header. There is no point outputting anything prior to a header call, move it. Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-483401 Share on other sites More sharing options...
Shiny_Charizard Posted March 5, 2008 Author Share Posted March 5, 2008 But I need to have the html on top so that if it tells the user that he was supposed to write something in the text field. Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-483414 Share on other sites More sharing options...
Shiny_Charizard Posted March 7, 2008 Author Share Posted March 7, 2008 *Bump* Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485617 Share on other sites More sharing options...
deadonarrival Posted March 7, 2008 Share Posted March 7, 2008 The header will direct the user away before they can read the html... Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485619 Share on other sites More sharing options...
Shiny_Charizard Posted March 7, 2008 Author Share Posted March 7, 2008 But the mysql query has to be done before the page redirects the user and if it redirects the user before then the mysql query won't get done. Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485682 Share on other sites More sharing options...
deadonarrival Posted March 7, 2008 Share Posted March 7, 2008 Do the mysql query, just don't output any html. Put this: #Display Page Heading echo "<center><b>Battle</b></center> <br /> <br />"; In this else: (Just before the following) #Display Message echo "Please choose a user to battle! <br /> <br />"; Same output, but it should work. (Assuming top.php doesn't output any code!) Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485698 Share on other sites More sharing options...
Baabu Posted March 7, 2008 Share Posted March 7, 2008 <script> setTimeout("location='index.php'", 3000); // Execute the statement location='index.php' after 3000 miliseconds (3 seconds) </script> try this out hope so it will work for u after your processing u can have this java script to take the user to required page Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485854 Share on other sites More sharing options...
roopurt18 Posted March 7, 2008 Share Posted March 7, 2008 Javascript is not a reliable solution because users can turn it off. Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485892 Share on other sites More sharing options...
haku Posted March 7, 2008 Share Posted March 7, 2008 I agree fully. That's not a good solution. Change your code to this: #If The User Has Submitted The Form if($_POST[submit]) { #If The Opponent ID Field Is Not Empty if(!empty($_POST[opponent])) { #Update The Mysql Table mysql_query("UPDATE members SET In_Battle?='Yes',opponent_id='$_POST[opponent]' WHERE user_id='$_SESSION[user_id]'"); #Redirects The User To The Battle File header("Location: battle.php"); } #If The Opponent ID Field Is Empty else { #Display Page Heading echo "<center><b>Battle</b></center> <br /> <br />"; #Display Message echo "Please choose a user to battle! <br /> <br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485922 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.