TheJoey Posted October 13, 2009 Share Posted October 13, 2009 What would be the best way to go about doing some server side validation on PHP? Any example would be great Quote Link to comment https://forums.phpfreaks.com/topic/177512-solved-form-validation/ Share on other sites More sharing options...
TheJoey Posted October 13, 2009 Author Share Posted October 13, 2009 Hey i need help with this i wrote this out <?php $displayEmailError = 'Email Must be Valid'; $displayAgeError = 'Age must be Valid'; $displayFirstError = 'First Name cant be Empty'; $displayLastError = 'Last Name cant be Empty'; if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) $displayEmailError = true; if (($_POST['Age']) <18 & > 67) $displayAgeError = true; if (empty($_POST['FirstName']) $displayFirstError = true; if (empty($_POST['LastName']) $displayLastError = true; if ($displayEmailError, $displayAgeError, $displayFirstError, $displayLastError) = false {save to database} but im getting syntax's alover the joint i need a litle help Parse error: syntax error, unexpected '>' Quote Link to comment https://forums.phpfreaks.com/topic/177512-solved-form-validation/#findComment-935937 Share on other sites More sharing options...
Coreye Posted October 13, 2009 Share Posted October 13, 2009 Try: <?php $displayEmailError = 'Email Must be Valid'; $displayAgeError = 'Age must be Valid'; $displayFirstError = 'First Name cant be Empty'; $displayLastError = 'Last Name cant be Empty'; if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) { $displayEmailError = true; } if ($_POST['Age'] < 18 OR $_POST['Age'] > 67) { $displayAgeError = true; } if (empty($_POST['FirstName'])) { $displayFirstError = true; } if (empty($_POST['LastName'])) { $displayLastError = true; } if (!$displayEmailError && !$displayAgeError && !$displayFirstError && !$displayLastError) { save to database } ?> Quote Link to comment https://forums.phpfreaks.com/topic/177512-solved-form-validation/#findComment-935947 Share on other sites More sharing options...
TheJoey Posted October 13, 2009 Author Share Posted October 13, 2009 No errors, but nothing seems to be happening. $displayEmailError = 'Email Must be Valid'; $displayAgeError = 'Age must be Valid'; $displayFirstError = 'First Name cant be Empty'; $displayLastError = 'Last Name cant be Empty'; if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) { $displayEmailError = true; } if ($_POST['Age'] < 18 OR $_POST['Age'] > 67) { $displayAgeError = true; } if (empty($_POST['FirstName'])) { $displayFirstError = true; } if (empty($_POST['LastName'])) { $displayLastError = true; } if (!$displayEmailError && !$displayAgeError && !$displayFirstError && !$displayLastError) { Quote Link to comment https://forums.phpfreaks.com/topic/177512-solved-form-validation/#findComment-936064 Share on other sites More sharing options...
TheJoey Posted October 13, 2009 Author Share Posted October 13, 2009 had to do it a different way, but i guess it still works fine. Quote Link to comment https://forums.phpfreaks.com/topic/177512-solved-form-validation/#findComment-936073 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2009 Share Posted October 13, 2009 Actually, if you use an array to hold the errors and set the index to be the type of error and set the value to be the error message, it makes both checking if there are no errors and displaying a specific error easier at the proper place when the form is redisplayed. $errors = array(); // define an empty array at the start .... $errors['EmailError'] = $displayEmailError; // set an error in the array ... // to check if there are no errors - if(empty($errors)){ // no errors, process the data } Quote Link to comment https://forums.phpfreaks.com/topic/177512-solved-form-validation/#findComment-936074 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.