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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.