Benny Andajetz
Benny Andajetz

Reputation: 123

PHP Web Form Error Display

I am a PHP newb. I have the following code for a web form. It works fine as is, but I would like to do the following:

  1. Return the errors as an array (?) so I can display errors as individual lines under each input.

and

  1. Disallow the form from being able to be submitted twice.

Any help would be greatly appreciated.

<form id="form1"  method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<fieldset>
<legend>Contact Me</legend> 
<?php 


    if (isset($_POST['Submit'])) {  

        if ($_POST['firstname'] != "") {  
            $_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);  
            if ($_POST['firstname'] == "") {  
                $errors .= 'Please enter a valid first name.<br/><br/>';  
            }  
        } else {  
            $errors .= 'Please enter your first name.<br/>';  
        }

           if ($_POST['lastname'] != "") {  
            $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);  
            if ($_POST['lastname'] == "") {  
                $errors .= 'Please enter a valid last name.<br/><br/>';  
            }  
        } else {  
            $errors .= 'Please enter your last name.<br/>';  
        }  

        if ($_POST['email'] != "") {  
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);  
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {  
                $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";  
            }  
        } else {  
            $errors .= 'Please enter your email address.<br/>';  
        }  


        if ($_POST['message'] != "") {  
            $_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);  
            if ($_POST['message'] == "") {  
                $errors .= 'Please enter a message to send.<br/>';  
            }  
        } else {  
            $errors .= 'Please enter a message to send.<br/>';  
        }  

        if (!$errors) {  
            $mail_to = '***@****.com';  
            $subject = 'New Mail from Web Site';  
            $message  = 'From: ' . $_POST['firstname'] . " " .  $_POST['lastname'] . "\n";  
            $message .= 'Email: ' . $_POST['email'] . "\n";                   
            $message .= "Message:\n" . $_POST['message'] . "\n\n";  
            mail($mail_to, $subject, $message);  

            echo "<p>Thank you for your email!<br/><br/></p>";  
        } else {  
            echo '<div style="color: #00CC00">' . $errors . '<br/></div>';  
        }  
    }

?>  

<label>First Name:</label>   
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" size="50" />

<label>Last Name:</label>  
<input type="text" name="lastname" value="<?php echo $_POST['lastname']; ?>" size="50" />    
<label>Email Address:</label>  
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/>    
<label>Message:</label>  
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>  
<br/>  
<input type="submit" class="moveright" name="Submit" value="Submit" /> 
</fieldset> 
</form> 

Upvotes: 0

Views: 3805

Answers (3)

imkingdavid
imkingdavid

Reputation: 1389

Instead of appending each error to the string, do like the following: $errors[] = 'error text';

EDIT: as the others have said, it's good practice to initialize the array before starting to set the values, like so: $errors = array();

As for the disallowing the form to be submitted twice, that needs javascript. Here's a link to help: http://www.webmasterworld.com/forum91/3781.htm

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270637

To make your errors into an array, initialize it before form processing as:

$errors = array();
if (isset($_POST['Submit'])) {  
...

Each time you have an error, rather than concatenating it on with .=, use the [] array append syntax:

$errors[] = 'Please enter a message to send.'; 

To prevent the form from being submitted twice, we often use a variable in $_SESSION to indicate that it has been completed. On successful submission, set a $_SESSION['success'] flag. Don't forget also to initialize the session at the start of the script:

session_start();
$_SESSION['success'] = FALSE;
$errors = array();

// Only process the form if the session flag isn't set:
if (isset($_POST['Submit']) && !$_SESSION['success']) {  
...
// Later, on success, 
echo "<p>Thank you for your email!<br/><br/></p>";  
// Set the flag to prevent resubmission.
$_SESSION['success'] = TRUE;

Upvotes: 1

Smamatti
Smamatti

Reputation: 3931

You can use an array for the errors instead of concatenating them into one string. Then you can check for each error at the specified form input.

Sample error check

// instead of: $errors .= 'Please enter a message to send.<br/>';
if ($_POST['message'] == "")
    $errors['message'] = 'Please enter a message to send.<br/>';

Sample error display

<label>Message:</label>
<?php if ($errors['message'] != "") echo $errors['message']; ?>
<textarea name="message" rows="5" cols="50"><?php echo $_POST['message']; ?></textarea>  

Upvotes: 2

Related Questions