stijn0713 Posted July 13, 2012 Share Posted July 13, 2012 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 https://forums.phpfreaks.com/topic/265612-redirects-too-fast-to-a-page-before-echoing-statement/ Share on other sites More sharing options...
trq Posted July 13, 2012 Share Posted July 13, 2012 You cannot send output and then redirect. You will need to redirect, and then display your message. Link to comment https://forums.phpfreaks.com/topic/265612-redirects-too-fast-to-a-page-before-echoing-statement/#findComment-1361261 Share on other sites More sharing options...
Barand Posted July 13, 2012 Share Posted July 13, 2012 Use a "meta refresh" tag? Link to comment https://forums.phpfreaks.com/topic/265612-redirects-too-fast-to-a-page-before-echoing-statement/#findComment-1361267 Share on other sites More sharing options...
ignace Posted July 13, 2012 Share Posted July 13, 2012 Or send a Refresh header. header('Refresh: 5,redirect.here'); Link to comment https://forums.phpfreaks.com/topic/265612-redirects-too-fast-to-a-page-before-echoing-statement/#findComment-1361269 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2012 Share Posted July 13, 2012 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 https://forums.phpfreaks.com/topic/265612-redirects-too-fast-to-a-page-before-echoing-statement/#findComment-1361275 Share on other sites More sharing options...
Anonim250 Posted July 14, 2012 Share Posted July 14, 2012 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 https://forums.phpfreaks.com/topic/265612-redirects-too-fast-to-a-page-before-echoing-statement/#findComment-1361441 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.