Jump to content

Small part of script not working


trg86

Recommended Posts

Hi there! I am having s very small portion of my form processing script that is not working and I can't quite put my finger on what is going on. All of the validation works fine, the only problem is to avoid processing a blank form, i.e. not completing any fields and clicking 'submit'

 

This is the part in my script that checks if any or all of the fields are blank and if they are it throws the error I have setup, if they are not blank (completed fields) then it continues on in processing the rest of the script.

// Check For Any Null Values, If Null Then Throw Error
if( !($name=='') && !($email=='') && !($phone_number=='') && !($contact_method=='') && !($phone_consent=='') && !($best_time=='') && !($referrer=='') && !($message==''))
{
$mailError = "";
}
else
{
THIS IS WHERE MY SCRIPT CONTINUES ON IF ALL OF THE FORMS ARE COMPLETED AND HAVE PASSED VALIDATION

IT PROCESSING THIS SECTION EVEN IF THE FIELDS ARE BLANK

As I have mentioned, it is processing an all blank field anyway and I am not quite sure why....I am a little stumped.

 

Thanks in advance for your help!

 

Link to comment
Share on other sites

EDIT: the whole thing is maybe a tad convoluted.

 

You could type coerce:

 

if ($name && $email && $phone_number && $contact_method && $phone_consent && $best_time && $referrer && $message) {
   $error_msg = '';
} else {
   handle_error_with_a_function_or_exception();
}
You could check for empty using OR:

 

if (empty($name) || empty($email) || empty($phone_number) || empty($contact_method)) {
   bad();
} else {
   good();
}
Also, end-users of the world would love it if you do them a favor and verify the form on the client-side prior to submission.
Link to comment
Share on other sites

Your logic is off. You're checking if all fields are empty, otherwise you accept the request. To check if any field is empty, you need the logical OR.

 

But the code generally doesn't make a lot of sense. The phrasing is weird, you don't tell the user what's actually wrong, and you haven't thought of the case that the whole parameter is missing.

 

A more useful approach:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // collect error messages
    $errors = [];

    // check if all required fields are present and not empty
    $required_params = ['name', 'email', 'phone_number', 'contact_method', /* ... */];
    foreach ($required_params as $param)
    {
        if (!isset($_POST[$param]))
        {
            // the parameter doesn't exist at all; this could be a client or a server error
            $errors[] = 'Missing parameter: '.$param;
            trigger_error('Missing POST parameter: '.$param, E_USER_NOTICE);
        }
        elseif (trim($_POST[$param]) == '')
        {
            // the value is empty
            $errors[] = 'Empty parameter: '.$param;
        }
    }

    if ($errors)
    {
        // handle errors
    }
    else
    {
        // continue
    }
}

Of course you should consider a more user-friendly error handling like going back to the form and displaying the error messages right next to the fields.

Link to comment
Share on other sites

trg86: You must be fucking kidding. You posted this exact code last week, got a detailed answer from mac_gyver, completely ignored it, then asked the question again, got another detailed answer, again ignored it, and now you have the same garbage code you had before.

 

How many more years do you want to spend running in circles? It looks like you've been doing that in the last five years.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.