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! Quote 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, Quote 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(); Quote 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!! Quote 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. Quote 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? Quote 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? Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.