1

I want to create a function that checks each filed, If empty add error messages for that field.

If I have the following form:

<form method="post">
    <input type="text" name="name" placeholder="Name">
    <input type="email" name="email" placeholder="Email">
    <input type="submit" name="submit" value="Submit">
</form>

I tried:

$errors = array();
function empty_check($var, $fieldName){
    if( empty($var) ){
        $error = $fieldName . ' can\'t be empty';
        array_push($errors, $error);
    }
}

if (isset($_POST['form_submit'])) {
    empty_check( $_POST['name'], 'Name' );
    empty_check( $_POST['email'], 'Email' );
    print_r($errors);
}

So if I submit the form and both fields are empty, I should get:

Array(
    [0] => Name can't be empty 
    [1] => Email can't be empty 
)

But I get error saying that the $errors inside empty_check function is not defined.

When I add it to the parameters:

function empty_check($var, $fieldName, $errors){
    ..
}

Then I need to add that parameter for each time I run the function, And I get an empty array:

empty_check( $_POST['name'], 'Name', $errors );
empty_check( $_POST['email'], 'Email', $errors );

That returns:

Array()

I tried return, But I get an error [] can't be used for reading:

function empty_check($var, $fieldName){
    if( empty($var) ){
        $errors[] = $fieldName . ' can\'t be empty';
        return $errors[];
    }
}

How should it be?

0

3 Answers 3

2

Personally I would write a SpamChecker() class that looked over bad data and empty data and verified the emails. What you are look for could be handled on the frontend with validation and you should have that anyway.

function check_for_empty_form_values($frm)
{
    $keys = array_keys($frm);
    if (empty($keys)) {
        return array("No data set");
    }
    $errors = array();
    foreach ($keys as $field) {
        if (empty($frm[$field])) {
            $errors[] = " Field : " . $field . " not set";
        }
    }
    return $errors;
}


$_POST["first_name"] = "Mike";
$_POST["last_name"] = "Q";
$_POST["middle"] = "";

$res = check_for_empty_form_values($_POST);

if (count($res) > 0) {
    echo " Errors Found " . count($res) . "\n";
}
var_dump($res);

Example command line output:

Errors Found 1
array(1) {
  [0]=>
  string(23) " Field : middle not set"
}
Sign up to request clarification or add additional context in comments.

2 Comments

I will validate the fields with other functions, I'm not very good at OOP PHP, So I can't write such class
@bon you should vote someone's answer if you like one of them... OOP php is fairly easy to learn IMO, it's worth it.
0

Variables from the global scope must be declared with the global before use in a function scope. Reference: Variable scope.

Therefore:

function empty_check() {
  global $errors;

  // ..
}

This is similar to using the extern keyword to declare global variables in a function scope in C. This is different from many newer languages like JavaScript, whose functions can inherit variables from the global scope.

2 Comments

I know the scope and global, But then won't the array rest to empty each time the functions is fired?
@Bon No, declaring the variable using global will not be resetting it. Using global only indicates that you want to be using the globally-scoped $errors and not make a local copy. Have you tried this code?
0
function empty_check($var, $fieldName,&$errors) {
 if( empty($var) ){
    $error = $fieldName . ' can\'t be empty';
    array_push($errors, $error);
   }
}

4 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
I mentioned that I tried that way, But what the & at the beginning of the 3rd parameter does?
@Bon basically that is a way to save memory by passing by reference. You don't need it. FYI: php.net/manual/en/language.references.pass.php
@Bon You need the & if you wish to use the changes to $errors outside of the function... without resorting to the horrendous use of global. The only other way is to pass the $errors back out in the return like in Mike's answer. However that can grow trickier if you also want to return something else out of a function (like a true/false if it the validation function was a success or failure)..... sooo this is where a Class object really helps, as you would be using $this->errors or self::$errors ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.