user1493448
user1493448

Reputation: 341

Which function should I use to detect empty values in a form POST?

I don't want to import blank/empty, space, null value on my database. So for this reason I want to check my input value before importing on database. Please any one can tell me isset and empty function which one is good for checking input value. here is my code. Thanks

$required = array('login', 'password', 'confirm', 'name', 'phone', 'email');

$error = false;
foreach($required as $field) {
  if (empty($_POST[$field])) {
    $error = true;
  }
}

if ($error) {
  echo "All fields are required.";
} else {
  echo "Proceed...";
}

Upvotes: 1

Views: 209

Answers (7)

Phil
Phil

Reputation: 473

You could incorporate isNotEmpty function

<?php
function isNotEmpty($input){
    $strTemp = trim($input);

    if($strTemp !== ''){
       return true;
    }
    return false;
}

?>

  • Save isNotEmpty as 'isnotempty.php' encase you need to reference it in the future.

    include 'isnotempty.php';
    $error = false;
    foreach($required as $field) {
      if (!(isNotEmpty($_POST[$field]))) {
        $error = true;
      }
    
    if ($error) {
      echo "All fields are required.";
    } else {
      echo "Proceed...";
    }
    

Hope this helps

Upvotes: -1

Lion King
Lion King

Reputation: 33813

Isset() checks if a variable has a value including ( Flase , 0 , or Empty string) , But not NULL.
Returns TRUE if var exists; FALSE otherwise.

empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.

Example:

$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}


// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}

The source

In your case, you can use empty() function

Upvotes: 1

Alastor
Alastor

Reputation: 357

Fix it:

foreach($required as $field) 
{
   $val = trim($_POST[$field];
   if (empty($val)) 
   {
      $error = true;
   }
}

Upvotes: 0

Deep Frozen
Deep Frozen

Reputation: 2075

The best way might be using empty and trim. Why trim? It will remove the spaces and lines etc. from the beginning and the end. That means, when someone inserts a few spaces but no text, the spaces can be removed so you can check empty:

if(empty(trim($foo))) 
{
    // It is empty!
}

Upvotes: 1

Jon
Jon

Reputation: 437386

isset is totally useless and empty is perhaps not an appropriate choice here.

The values inside $_POST (and also $_GET and $REQUEST) are always typed as strings, so isset will always return true. Additionally, the behavior of empty on false, null and other such values does not come into play, which means that the empty check will only reject:

  • zero-length strings
  • the string "0"

This is different from what your code seems to intend to reject, which would be:

  • zero-length strings
  • strings composed entirely of whitespace

Consider using trim($_POST['foo']) === '' as the condition instead.

Upvotes: 1

user399666
user399666

Reputation: 19879

$required = array('login', 'password', 'confirm', 'name', 'phone', 'email');

$error = false;
foreach($required as $field) {
  if (empty($_POST[$field]) || strlen(trim($_POST[$field])) == 0) {
    $error = true;
  }
}

if ($error) {
  echo "All fields are required.";
} else {
  echo "Proceed...";
}

Upvotes: 1

Thomas Glaser
Thomas Glaser

Reputation: 2167

In this situation empty makes more sense, because it also checks whether the string is empty. isset just checks whether if it is defined. You might also want to trim the input.

Upvotes: 1

Related Questions