merylvingien Posted November 14, 2009 Share Posted November 14, 2009 Hi guys, i have a form with a nifty bit of ajax code going on that checks a email address is valid. This works out ok, and if the email is valid it displays a hidden field with a true value or if it is invalid it displays different hidden field with a false value. This value gets carried over to the next page. The trouble is, if a user fills out some of the form including the email field and then for some reason refreshes the page, the hidden field is no longer shown, along with the valid message or invalid message and on the next page where the value is posted there is nothing. I have tried if (empty($emailcheck)) {die etc and that stops the user going any further, but i still get a Notice: Undefined index: emailcheck I have also tried if (empty($emailcheck)) {$emailcheck == "false";} But that dont work either... here is the code as it is at the moment: $emailcheck = $_POST['emailcheck']; if (empty($emailcheck)) {die ('<p>There appears to be an error with the email validation, please go back and re-enter your email address</p>');} else if ($emailcheck == 'false') {die ('<p>It appears that you have entered an invalid email address, please hit the back button in your browser and check that you have entered a valid email address.</p>');} else { How can i get round this notice? Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/ Share on other sites More sharing options...
Mchl Posted November 14, 2009 Share Posted November 14, 2009 Try isset instead Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957399 Share on other sites More sharing options...
merylvingien Posted November 14, 2009 Author Share Posted November 14, 2009 Tried it LOL if (isset($_POST['emailcheck']) as $emailcheck) { if ($emailcheck == 'false') {die ('bla bla');} } else Like that you mean? I tried that out before coming here, if it is not set, then it just carries on as normal, without checking if the email is valid. Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957403 Share on other sites More sharing options...
mga_ka_php Posted November 14, 2009 Share Posted November 14, 2009 try using $_SESSION Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957407 Share on other sites More sharing options...
rarebit Posted November 14, 2009 Share Posted November 14, 2009 See specifics here... http://uk2.php.net/manual/en/function.isset.php But i'd write that code like: if (!isset($_POST['emailcheck'])) {die ('bla bla');} else {} Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957411 Share on other sites More sharing options...
merylvingien Posted November 14, 2009 Author Share Posted November 14, 2009 rarebit that part works, i just can't get the other bit to work now. Should this work? Cause it dont.. if (isset($_POST['emailcheck'])) { $emailcheck = 'emailcheck'; if ($emailcheck == 'false') {die ('<p>bla bla bla</p>');} } else if (!isset($_POST['emailcheck'])){die ('bla bla');} else { Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957488 Share on other sites More sharing options...
mrMarcus Posted November 14, 2009 Share Posted November 14, 2009 the reason it disappears is because the AJAX was run in order to get that hidden input. when the user refreshes the page, the AJAX then needs to be run again to re-obtain that value. just do a valid email check in the form processing script. this way, if somebody refreshes the page, the process script will catch them. you should always have a secondary line of defense with PHP. AJAX/javascript is all fine and dandy, it looks nice and can be a great tool for form efficiency, but it can be easily manipulated (as well as turned off), so don't ever, ever count on it. Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957490 Share on other sites More sharing options...
merylvingien Posted November 14, 2009 Author Share Posted November 14, 2009 mrMarcus thats what i am doing. I solved the problem anyway, its all works fine now. the solution was if (!isset($_POST['emailcheck']) || $_POST['emailcheck'] === 'false'){die ('bla bla');} else Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957492 Share on other sites More sharing options...
mrMarcus Posted November 14, 2009 Share Posted November 14, 2009 yes, but you were wondering why the hidden value was disappearing. you sounded like you counting on that value being there which isn't a good thing. and just use: if ((!isset($_POST['emailcheck'])) || (empty ($_POST['emailcheck']))) empty() will check if it's FALSE. Link to comment https://forums.phpfreaks.com/topic/181498-solved-if-empty/#findComment-957496 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.