How to use php empty function for this?

Good morning. Below is how my php formmail script processes my spam question. Whats the simplest way to write the same type of thing but only checking for if “name, phone, and email” inputs are NOT left empty? Thanks

if ($spam == 4){
    // success, continue processing
}
else{
    header( "Location: $errorurl" );
	exit ;
}

Hi there PicnicTutorials,

have you not considered just using the

HTML attribute: required

coothead

Check out the free Online PHP Manual for the following:

// PHP 7 ==> Null coalescing operator
$tmpName = $_POST['name'] ?? NULL; // FALSE, 'EMPTY'

isset(...)
empty(...)
var_dump(...);

$ok = $tmpName && $tmpPhone && $tmpEmail;
if( $ok ) : 
   // echo '<br>$ok line ==> ' .__line__;
else:
   // echo '<br>$PROBLEM line ==> ' .__line__;
endif;

html required - doesn’t get any easier than that does it. But to keep with my jquery validation styling and php validation already in place its got to be php as the back up validation.

like how would I just write…

if ($name empty) {
header( “Location: $errorurl” );
exit ;}
}

I know thats not correct obviously but you get what I’m after.

What you do after checking the REQUEST METHOD you trim the POST array all at once, then you check if the particular fields are empty, If they are, you put the errors in an error array. Just past that, you check the errors array and if it is not empty, you loop over each of the errors and output them to the user.


$error = [];

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

// Trim POST array here....

    if (empty($_POST['name']))
    {
        $error['name'] = 'Name Required.';
    }

    if (empty($_POST['phone']))
    {
        $error['phone'] = 'Phone Required.';
    }

    if ($error)
    {
        // Handle errors
    }
    else{
        //Processs Form
    }
}
1 Like

Is that right? I’m sure there is something wrong with it

$name = $_POST['name'] ?? NULL; 
$phone = $_POST['phone'] ?? NULL;
$email = $_POST['email'] ?? NULL; 

isset(...)
empty(...)
var_dump(...);

$ok = $name && $phone && $email;
if( $ok ) : 
   // continue processing
else:
  header( "Location: $errorurl" );
	exit ;
endif;

Try the three referenced php functions to see the results