mrherman Posted June 26, 2011 Share Posted June 26, 2011 I need to learn some basics about sending a message back to the user concerning the form fields that the user omitted in a login form. So far, I've tried placing the error message in a $_POST array variable, but when I redirect the user back to the login form, the $_POST message is not available. My structure is: index.php --> login_form.php --> process_form.php --> (fields omitted) --> login_form.php Is there a tutorial that addresses this common procedure? I used phpFreaks search, but couldn't come up with a solution from existing posts. Probably my search was not well-formed. Thanks! Link to comment https://forums.phpfreaks.com/topic/240463-sending-error-message-back-to-user-who-omitted-fields-in-login-form/ Share on other sites More sharing options...
WebStyles Posted June 26, 2011 Share Posted June 26, 2011 $_POST is only available when you submit a form. you need to put the variables in $_SESSION, that will make them available on every page, as long as you add session_start(); to the beginning of every page, before any output. Hope this helps, Link to comment https://forums.phpfreaks.com/topic/240463-sending-error-message-back-to-user-who-omitted-fields-in-login-form/#findComment-1235109 Share on other sites More sharing options...
mgoodman Posted June 26, 2011 Share Posted June 26, 2011 As WebStyles said, you have to use $_SESSION. Here's a simplified class that can handle flash messages for you: class FlashMessage { public static function wirte($message) { self::_startSession(); $_SESSION['flash.message'] = $message; } public static function check() { self::_startSession(); if (isset($_SESSION['flash.message'])) { $m = $_SESSION['flash.message']; unset($_SESSION['flash.message']); return $m; } else { return null; } } protected static function _startSession() { if (session_id() === '') { session_start(); } } } To use it you just do this in process_form.php FlashMessage::write('You missed some fields!'); and then do this on login_form.php: echo FlashMessage::check(); Link to comment https://forums.phpfreaks.com/topic/240463-sending-error-message-back-to-user-who-omitted-fields-in-login-form/#findComment-1235110 Share on other sites More sharing options...
mrherman Posted June 26, 2011 Author Share Posted June 26, 2011 Well, thank you both. Those simple facts and the code help quite a bit!! Link to comment https://forums.phpfreaks.com/topic/240463-sending-error-message-back-to-user-who-omitted-fields-in-login-form/#findComment-1235129 Share on other sites More sharing options...
Pikachu2000 Posted June 26, 2011 Share Posted June 26, 2011 Or you can simply use one script to both display and process the form. Then you don't have to pass error messages between different scripts at all. Link to comment https://forums.phpfreaks.com/topic/240463-sending-error-message-back-to-user-who-omitted-fields-in-login-form/#findComment-1235132 Share on other sites More sharing options...
mrherman Posted June 26, 2011 Author Share Posted June 26, 2011 Thank you, Pikachu2000...but doesn't your byline suggest that this is bad? Link to comment https://forums.phpfreaks.com/topic/240463-sending-error-message-back-to-user-who-omitted-fields-in-login-form/#findComment-1235155 Share on other sites More sharing options...
PFMaBiSmAd Posted June 26, 2011 Share Posted June 26, 2011 Who said that you must use PHP_SELF to get a form to submit to the same page? Link to comment https://forums.phpfreaks.com/topic/240463-sending-error-message-back-to-user-who-omitted-fields-in-login-form/#findComment-1235158 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.