bcart Posted November 10, 2009 Share Posted November 10, 2009 Hi there I am trying to make a very simple form validation function. I currently have the following function formValidate($field, $msg) { if (empty($_POST['$field'])) { $errors[] = $msg; } else { $field = mysqli_real_escape_string($dbc, trim($_POST['$field'])); } }// end of function formValidate('form', 'You forgot to enter the form'); When I run the script and enter nothing into the $_POST['form'] field it still executes an empty record into the database. What am I doing wrong? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/180985-form-validation-with-functions/ Share on other sites More sharing options...
gevans Posted November 10, 2009 Share Posted November 10, 2009 I'm guessing it;s because $errors is local to your function. If you test for $errors outside of the function it will say it's not set, because it isn't in that scope. Quote Link to comment https://forums.phpfreaks.com/topic/180985-form-validation-with-functions/#findComment-954867 Share on other sites More sharing options...
KevinM1 Posted November 10, 2009 Share Posted November 10, 2009 You have to return the escaped value from the function. Also, your $errors array won't work the way you want it to due to scope. function formValidate($field) { if(empty($_POST[$field])) { return false; } else { $field = mysqli_real_escape_string(trim($_POST[$field])); return $field; } } $result = formValidate('name'); if(!$result) { echo "You forgot to enter your name!"; } Quote Link to comment https://forums.phpfreaks.com/topic/180985-form-validation-with-functions/#findComment-954873 Share on other sites More sharing options...
Garethp Posted November 10, 2009 Share Posted November 10, 2009 function Validate($Input, $Message) { global $errors; if(!preg_match('~[\S]~', $Input)) { $errors[] = $Message; return $Input; } else { $Input = mysqli_real_escape_string($dbc, $Input); return $Input; } } $V = Validate($_POST['form'], 'You forgot to enter the form'); if(sizeof($errors) > 0)) { //The fields were empty } You need the global $errors; Quote Link to comment https://forums.phpfreaks.com/topic/180985-form-validation-with-functions/#findComment-954875 Share on other sites More sharing options...
KevinM1 Posted November 10, 2009 Share Posted November 10, 2009 Global is bad. Do not use global. Either pass the errors array to the function via the argument list, or don't use it at all. Quote Link to comment https://forums.phpfreaks.com/topic/180985-form-validation-with-functions/#findComment-954877 Share on other sites More sharing options...
KevinM1 Posted November 10, 2009 Share Posted November 10, 2009 A more eloquent solution to the OP that shows how passing the errors array into the function can work: $errors = array(); function formValidate($field, $msg, &$errorArray) { if(empty($_POST[$field])) { $errors[] = $msg; return false; //since we're returning a legit value later on in the function, I like to return a false/null value in an error condition } else { $field = mysqli_real_escape_string(trim($field)); return $field; } } $result = formValidate('name', 'You must enter your name!', $errors); if(count($errors) > 0) { //handle errors } else { //insert $result } I didn't mention it earlier since the OP had a problem with both return values and scope. I figured passing an argument by reference may be too advanced for their current skill level, but since the idea of using global to 'fix' the problem came up, I figured it's best to show the right way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/180985-form-validation-with-functions/#findComment-954894 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.