user2495761
user2495761

Reputation:

PHP Not Reading Form Fields As Empty

everyone! I've been having a terrible time trying to figure out why my form validation doesn't recognize empty fields and return them as errors.

Here is a function I use to check the fields:

function check_required_fields($required_array) {
$field_errors = array();
foreach($required_array as $fieldname) {
    if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { 
        $field_errors[] = $fieldname; 
    }
}
return $field_errors;
}

Then, my form is process like this:

if (isset($_POST['submit'])) { // Form has been submitted.
    $errors = array();

    // perform validations on the form data
    $required_fields = array('client', 'banner', 'district', 'store', 'position', 'first_name', 'last_name', 'email', 'password');
    $errors = array_merge($errors, check_required_fields($required_fields, $_POST));

and an IF statement checks for errors:

if ( empty($errors) ) { 
        $query =

The problem is, even if my fields are empty (and I checked them with var_dump($_POST)) PHP performs the $query. I think there is something wrong with the function. I hope someone can help me spot the error!

Here is the gist of my HTML code:

<form action="registration.php" method="post">

<label for="first_name" class="medium">First Name</label>
<input type="text" name="first_name" class="textbox short_field">

<button type="submit" name="submit" value="submit" class="form-submit-button">SUBMIT</button>

</form>

I also use the following functions to validate string length and it works just fine:

function check_max_field_lengths($field_length_array) {
$field_errors = array();
foreach($field_length_array as $fieldname => $maxlength ) {
    if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname;
    }
}
return $field_errors;
}

and

$fields_with_lengths = array('first_name' => 30, 'last_name' => 30, 'email' => 50, 'password' => 30);
    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

Upvotes: 1

Views: 210

Answers (1)

user4035
user4035

Reputation: 23729

"The problem is, even if my fields are empty (and I checked them with var_dump($_POST)) PHP performs the $query."

Your problem is caused by this condition: $var != 0. Php can cast variables of different types between each other, so empty string is equal to 0. Proof:

<?php
$var = '';
var_dump($var == 0);

Output: bool(true)

Now look at this sample, imitating, how php processes your condition:

<?php
$var = '';
var_dump(empty($var));
var_dump((empty($var) && $var != 0));

Output:

bool(true)
bool(false)

In the 2-nd case the boolean function returns false, therefore, the variable won't be added to the array of errors.

I don't know, what do you want to achieve with this: $var != 0. But the condition:

if (!isset($_POST[$fieldname]) || 
    (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { 
      $field_errors[] = $fieldname; 
}

can be easily switched to this:

if (empty($_POST[$fieldname])) { 
     $field_errors[] = $fieldname; 
}

Upvotes: 1

Related Questions