Screenflicker

home

A Better Email Form with PHP

24 Oct 2005

As promised, I'd like to follow up on my post about building a better email form with PHP. I received a few responses from some great people and with their help I was able to work through my problems and get this form working as intended.

Hiccup number one

The majority of the problem was actually in the checkRequired() function. Since that is where the brunt of the work is actually done, it only makes sense. A good portion at the start of the function is still the same — including setting the variables, using array_push to create the $stack array with all the form values, and building the $arrRequired array. The first line that has been changed was this:

$arrErrors[$i]      =       $arrRequired[$i];

What I was trying to do here is push the required field into an array of errors during the loop. My logic was right but my technique was off. I still need to create an array from the required fields that are blank. With the line above, even if a field was null, which for some reason I always ended up with at least one null field (I think it has something to do with the form values that are submitted), it would return an error, thus the form would not be emailed — even if all the required fields were filled out correctly.

The fix? Remove the entire line. Since I removed that line I was no longer building the error array, which was very important to the success of this function. The question became, where do I build $arrErrors. The solution was in the conditional statement immediately following the line of code I had just removed.

if ( !is_null($arrRequired[$i]) )
{
    array_push( $arrErrors, $arrRequired[$i] );
    echo "<p class="error">Please note: 
                 ".$arrRequired[$i]." is a required field.</p>";
}

By changing the conditional above I was eliminating the null field which was stopping my email from sending and building the $arrErrors if there were any. This bit of code also displayed a nice message informing the person sending the form which field was causing the error.

Hiccup number two

As my code was at that time, there was no way for the values of $arrErrors to actually be viewed, edited or checked — nothing was being returned from the function. The quick fix turned out to be:

if ( $arrErrors )
{
    return $arrErrors;
}
else
{
    return;
}

This little conditional was placed right before the closing bracket of the function. It's sole purpose is to check if there are any values in $arrErrors. If there is any value in the array it will return the value of the error array, if the array is empty, it will return a null value.

Finishing it off

The final step was to put the actual error array values from the function into a variable I could check — for the sake of convenience, let's call this variable $errors. By creating this variable I am able to create a conditional statement that tells me if there are any errors. If that variable ($errors) is empty, which it would be if all the required fields are filled out, then we can send the email. So the final bit of code looks like this:

$errors = checkRequired();

if ( !$errors )
{
    //  Set the email variables here
            
    //  Send the email to each recipient
         foreach ($recipient as $send_to) {
        mail ($send_to, $topic, $text, $headers);
    }
        echo "<p>Thank you for contacting us. 
                 We will respond to your email as soon as possible.</p>";
    }
}

And there you have it. The form checks the required fields, as specified by a hidden input field in the form, puts any empty required fields into an error array and displays a message to the user or sends an email if all required fields are filled out.

I've put a little file together that you can download instead of doing the copy-paste thing. If you have any comments, questions or suggestions I'd be happy to hear them. Until then, keep your stick on the ice.

comments powered by Disqus