TrueColors Posted November 15, 2010 Share Posted November 15, 2010 I have Gender, Date of Birth, and Captcha validations. Though they are not working. Lets start with gender. Then captcha, then DOB. I call the Gender check via doing if(user::isValidGender($this->gender) == FALSE) $errors[] = "Invalid gender"; "user" is another class. The function isValidGender() is as follows public function isValidGender($gender) { $validGenders = array("male", "female", "mtf", "ftm", "androgyne", "intersex"); if(in_array($gender, $validGenders) == FALSE) return false; } When I do if(in_array($gender, $validGenders) == FALSE) die("error"); it doesn't die. It displays "Invalid Gender" which means that if it doesn't do the die() it returns true. Though it checks if the function returns false when calling "isValidGender()". If I put the die() above the if, it dies. So it obviously is returning true, but why is it displaying the error? Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/ Share on other sites More sharing options...
chintansshah Posted November 15, 2010 Share Posted November 15, 2010 Hey, Can you post an error here. Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/#findComment-1134293 Share on other sites More sharing options...
trq Posted November 15, 2010 Share Posted November 15, 2010 This will now return true if the given gender is valid, or false otherwise. public function isValidGender($gender) { $validGenders = array("male", "female", "mtf", "ftm", "androgyne", "intersex"); return in_array($gender, $validGenders); } Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/#findComment-1134294 Share on other sites More sharing options...
TrueColors Posted November 15, 2010 Author Share Posted November 15, 2010 Thanks thorpe that worked! Can you explain why my original method didn't work? - My next problem is the captcha. md5($captcha) is 706a335b65ce9ccabd6c683b2dec13d2 $_SESSION['ckey'] is 706a335b65ce9ccabd6c683b2dec13d2 (I echo'd them both out and they are exact) I call the function by doing this if(user::isValidCaptcha($this->captcha) == FALSE) $errors[] = "Your captcha image was incorrect!"; The function is public function isValidCaptcha($captcha) { die(md5($captcha) . '<br />' . $_SESSION['ckey']); // This was to test and both results were the same if(md5($captcha) != $_SESSION['ckey']) return false; } Even though both are the same, why does it display the error that it's incorrect? Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/#findComment-1134296 Share on other sites More sharing options...
chintansshah Posted November 15, 2010 Share Posted November 15, 2010 I don't find any error at my end.. I think, it's a correct code. Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/#findComment-1134303 Share on other sites More sharing options...
TrueColors Posted November 15, 2010 Author Share Posted November 15, 2010 I don't find any error at my end.. I think, it's a correct code. Clearly you are wrong when it's displaying "Your captcha image was incorrect!" Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/#findComment-1134310 Share on other sites More sharing options...
TrueColors Posted November 15, 2010 Author Share Posted November 15, 2010 return ((md5($captcha) == $_SESSION['ckey']) ? true : false); That method works. Again I ask, along with my previous issue: Why did it not work the way I had coded it? My next issue, which is slightly different. Is for DOB. I call it the same way and this is the function public function isValidDOB($day, $month, $year) { if(($day > 31 || $day < 01) || ($month > 12 || $month < 1) || ($year >= date('Y', time()) || $year <= 1910)) { return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/#findComment-1134317 Share on other sites More sharing options...
TrueColors Posted November 15, 2010 Author Share Posted November 15, 2010 Wait, if I only have return False- this means... as there is no return true, no value being emitted, it's returning false. Is that why they aren't working? Quote Link to comment https://forums.phpfreaks.com/topic/218689-form-validation-problems/#findComment-1134319 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.