Jump to content

Redirecting Problem.


Shiny_Charizard

Recommended Posts

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');
?>

 

Link to comment
https://forums.phpfreaks.com/topic/94390-redirecting-problem/
Share on other sites

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!)

Link to comment
https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485698
Share on other sites

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 />";
}
}

Link to comment
https://forums.phpfreaks.com/topic/94390-redirecting-problem/#findComment-485922
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.