Jump to content

redirects too fast to a page | before echoing statement


stijn0713

Recommended Posts

Hello,

 

i make a succesfull query to the db where i write away a new 'examination'.

 

This examination has subgroups, so after adding an examination, i wanted to redirect the people to the page for adding a subexamination.

 

I wrote this on the page: examination.php

 

$id = mysql_insert_id();

 

echo "Nieuw onderzoek ".$id." is aangemaakt!";

 

if($id){

sleep(3);

$_SESSION['ID_Ond'] = $id;

 

header("Location: GroepAanmaken.php" );

}

}

 

However, it won't echo the "examination succesfully added", but does redirects after 3 seconds. how come?

Thanks alot in advance

 

The reason your existing code doesn't actually output the echoed message to the browser before it redirects is because your php.ini has output_buffering enabled, so the output you are sending is being held in the buffer, then discarded when the header() redirect occurs.

Here is how it is typically done (simplified):

 

...
$message = "Nieuw onderzoek ".$id." is aangemaakt!"; 
FlashHelper::setFlash($message, FlashHelper::MESSAGE_TYPE_SUCCESS);
header("Location: GroepAanmaken.php" );
exit;
...

 

inside GroipAanmaken.php:

 

$flash = FlashHelper::flash();
if($flash != null)
  echo $flash;

 

FlashHelper.php:

 

class FlashHelper
{
const MESSAGE_TYPE_DEFAULT = 1;
const MESSAGE_TYPE_SUCCESS = 2;
const MESSAGE_TYPE_ERROR = 3;

public static function setFlash($message, $messageType = self::MESSAGE_TYPE_SUCCESS)
{
	$classCode = '';

	switch($messageType)
	{
		case self::MESSAGE_TYPE_DEFAULT:
			$classCode = '';
			break;
		case self::MESSAGE_TYPE_SUCCESS:
			$classCode = 'class="success"';
			break;
		case self::MESSAGE_TYPE_ERROR:
			$classCode = 'class="errorMessage"';

	}
	$_SESSION['flash_message'] = sprintf('<p %s>%s</p>', $classCode, htmlspecialchars($message));
}

public static function flash()
{
	if(!isset($_SESSION['flash_message']))
		return null;

	$message = $_SESSION['flash_message'];
	unset($_SESSION['flash_message']);
	return $message;
}
}

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.