Jump to content

Redirect to the referrer page


shujjah

Recommended Posts

Alright After submitted a comment it just shows that congrats that your comment has been added but I want is to redirect to the page from which the user submitted the comment like for e.g he submitted the comment on xxx.com/news.php?id=9 so after submitting the comment I want it to redirect it back to the same page after maybe 2-3 secondsthis is the script

<?php	include('functions.php');	if ($_POST['Comment']=='' || strlen($_POST['Comment'])<10)	{		$errors[] = 'Comment is required and must contain 10 characters or more';	}	if(is_array($errors))	{		echo '<p class="error"><b>The following errors occured</b></p>';		while (list($key,$value) = each($errors))		{			echo '<span class="error">'.$value.'</span><br />';		}	}	else {		//Proccesing the Comment		$link = mysql_connect("localhost","xxxx","xxxx");		mysql_select_db("speedyz_gamecms",$link);		$timestamp = date("Y-m-d G:i:s");				$sql = mysql_query("INSERT INTO gamecms_comment (assetId,userid,section,comment,userName,email,timestamp,auth,deleted)		VALUES ('{$_POST['assetId']}','{$_POST['id']}','{$_POST['section']}','".addslashes($_POST['Comment'])."','{$_POST['name']}','{$_POST['email']}','$timestamp','1','0')",$link) or die (mysql_error());		//Finish the Procces of the Comment		echo '<p><b>Success!</b></p>';		echo '<span>Your comment was successfully processed. Thank you for your comment!</span>';	}?>

Link to comment
Share on other sites

Hey mate,If the you are getting the comment from a form all you have to do is add this as the action attribute of the form:action="<?php echo $_SERVER['PHP_SELF']; ?>"Unless I misunderstood your question
No there is a from whose action is the script I posted and that script checks for errors and then adds the values into the database then echo's a success message what I want is after the success message to redirect the user back to the form page which has the news and shows the news and also has a form for adding commentsthe comment form and processing script are two different scripts
echo '<p><b>Success!</b></p>';		echo '<span>Your comment was successfully processed. Thank you for your comment!</span>';	}

what should I add after this to redirect the user back to the form page ( news page ) after like 2-3 secondsThanks for helping me

PHP to redirect immediately.
but I dont know what value to put in the header function ?
Link to comment
Share on other sites

this is the script now

<?php	include('functions.php');	if ($_POST['Comment']=='' || strlen($_POST['Comment'])<10)	{		$errors[] = 'Comment is required and must contain 10 characters or more';	}	if(is_array($errors))	{		echo '<p class="error"><b>The following errors occured</b></p>';		while (list($key,$value) = each($errors))		{			echo '<span class="error">'.$value.'</span><br />';		}	}	else {		//Proccesing the Comment		$link = mysql_connect("localhost","xxxxxx","xxxxxxx");		mysql_select_db("xxxx",$link);		$timestamp = date("Y-m-d G:i:s");				$sql = mysql_query("INSERT INTO gamecms_comment (assetId,userid,section,comment,userName,email,timestamp,auth,deleted)		VALUES ('{$_POST['assetId']}','{$_POST['id']}','{$_POST['section']}','".addslashes($_POST['Comment'])."','{$_POST['name']}','{$_POST['email']}','$timestamp','1','0')",$link) or die (mysql_error());		//Finish the Procces of the Comment		echo '<p><b>Success!</b></p>';		echo '<span>Your comment was successfully processed. Thank you for comment!</span>';				$referer = $_SERVER['HTTP_REFERER'];				header("Location: $referer");			   ?><a href="<?php echo $referer?>" > GO Back </a><?phpexit();	}?>

the A href works fine it redirects to the page from where the user came from ( just used it as a test to see whether $referer is working or not ) but the header is giving this error Warning: Cannot modify header information - headers already sent by (output started at /home/speedyz/public_html/comments/register.php:26) in /home/speedyz/public_html/comments/register.php on line 29

Link to comment
Share on other sites

Quoting from the php manual about headers.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...