简体   繁体   中英

I need to add required form fields to my contact form but not sure about how to do it?

I am very new to php and so I need some help adding required fields to my contact form. I want to display a nice "error" message telling the viewer to input something into the required field.

Here is my php script:

<?php
        if(isset($_POST['submit'])){

            $to = "benlevygraphics@gmail.com";
            $headers = "From: " . $_POST['email'];
            $subject = "Ben, you have been contacted...";
            $body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\n" . "\nFavorite Piece of work: " . $_POST['favwork'] . "\n" . "\nMessage: " . $_POST['message'];

            if(mail($to, $subject, $body, $headers)){

            echo("<p class=contactformsent>".$_POST['name'].", your information was received. For your records an email has also been sent to you!</p>");

            }
            else{
               echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
            }
       }

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

            $to = $_POST['email'];
            $headers = "From: benlevygraphics@gmail.com" ;
            $subject = "You contacted benlevywebdesign";
            $body =  $_POST['name'].",  Thank you for taking a look at my portfolio and contacting me.  I have received your contact information/message and I will get back to you as soon as I can!" . "\n" . "\nFor your records I have:" . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\n" . "\nFavorite Piece of work: " . $_POST['favwork'] . "\n" . "\nMessage: " . $_POST['message'];

            if(mail($to, $subject, $body, $headers)){
            }
       }

?>

and here is the html code:

<?php include("contactform.php"); ?>

<form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<fieldset>
            <p class="form">Name*
                <input type="text" name="name" id="name" size="30"/>
            </p>
            <p class="form">Email*
                <input type="text" name="email" id="email" size="30" />
            </p>...(the rest of the form html code here)

and then the last lines:

 <p class="submit"><button name="submit" type="submit">Send</button></p>

    </form>

At the Servers side you can just check for:

if(isset($_POST['name'])) { } // only send the mail if this is true.

There are also many client side solutions. You could also write your own using Javascript. You will have to still include the checks on the server-side tho since "client-side security does not work".. The user could just disable Javascript and submit the form without the checks running.

Here is an example for a JS library that does form validation:

http://rickharrison.github.com/validate.js/

I found it by googling for javascript form validation

In general, you should be escaping any input from the browser to prevent bad things from happening if you're going to ever display it in a browser or save it to a database.

Before the the following line:

if(mail($to, $subject, $body, $headers)){

You would need to add some validation to the form like so:

$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];

if (empty($name)) {
    $errors['name'] = 'Please enter your name!';
}

if (empty($email)) {
    $errors['email'] = 'Please enter your e-mail!';
}

Then change your email line to something like:

if(!count($errors) && mail($to, $subject, $body, $headers)){

Then instead of:

echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");

You could add an extra error like so if mail fails:

$errors['sendmail'] = 'Unable to send the e-mail!';

Additionally, you would just look for any errors on the form itself and loop through them at the top or under each input:

foreach ($errors as $error) {
    print $error."<br />\n";
}

Of course, this is just a start; you should definitely do some additional checks. For example, checking minimum length on name, stripping it of weird characters maybe and so on.

UPDATE

Ben, based on your comments, it seems like you want to do validation before the POST even happens. If this is the case, I'd recommend doing some basic jQuery validation.

Here's a page on Basic jQuery Validation with an example.

Is that more what you're getting at?

Additionally, if you want the page to return to your form after you submit the comment, make sure your form URL has the target tag to your form like so:

<a name="comments_form" />
<form id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>#comments_form" method="POST">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM