/    Sign up×
Community /Pin to ProfileBookmark

Contact Form With Validation And Error Messages

Hello everybody!

As I’m not an expert in php, I’m having some problems about creating a contact form.

I have the following form:

[code=html]
<form action=”invio.php” method=”post” enctype=”multipart/form-data” name=”modulo”>
<input type=”text” name=”name_lastname” size=”25″ class=”Text”>(requested)
<input type=”text” name=”city” size=”21″ class=”Text”>(requested)
<select name=”country” class=”Text”>(requested)
<option value=”” selected>Scegli il paese</option>
(lots of options tags….)
</select>
<input type=”text” name=”email” size=”55″ class=”Text”>(requested)
<input type=”text” name=”phone” size=”31″ class=”Text”> (not requested)
<input type=”text” name=”subject” size=”96″ class=”Text”>(requested)
<textarea cols=95 rows=8 name=”comments” class=”Text”></textarea>(requested)
<input name=”submit” type=”submit” value=”Invia”>
<input type=”reset” value=”Cancella”>
</form>[/code]

I would like to:

  • Error messages appear as a pop up window if the requested camps were not filled.

  • Validate the form sending the informations to my e-mail

  • Redirect the user to the “Thank You” page.
  • Does anyone can help me?

    I would be really glad.

    to post a comment
    PHP

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @bokehJul 21.2005 — At the minute all you have to go on is an html form. The pop up windows or alerts would be javascript. Using PHP for validation requires a page reload but is more conclusive. You also need to make your form sticky. What you are asking is easy but labour intensive. Maybe you should start writing and then come back when you run into trouble. By the way your form is partly set for file uploads. Is that what you will be doing?
    Copy linkTweet thisAlerts:
    @NogDogJul 21.2005 — Here's a formmail script I put together a while back. Feel free to adapt it to your needs, or just use it for ideas. (Note variables near the beginning you need to set to get the email to go where you want it to go.)
    [code=php]
    <?php
    ################################################################################
    #
    # mail.php - general-purpose email form and script
    #
    # copyright June 2005 by Charles Reace
    #
    # This software available as open source software under the Gnu General Public
    # License: http://www.gnu.org/licenses/gpl.html
    #
    ################################################################################

    ################################################################################
    # user-defined variables - update for your email address(es)
    ################################################################################

    # home page for link at top of page:
    $home = "http://www.your-site.com/";

    # Modify the following variables to set up where emails will be sent.
    # $toName will be concatenated with a "@" and then $toDomain to create
    # the complete email address.
    $toName = "name"; # the part that goes before the @
    $toDomain = "domain.com"; # the part that comes after the @

    # uncomment and edit the next two lines if you want to cc someone:
    # $ccName = "name";
    # $ccDomain = "domain.com";

    # uncomment and edit the next two lines if you want to bcc someone:
    # $bccName = "name";
    # $bccDomain = "domain.com";

    #################################
    # end of user-defined variables #
    #################################

    # email address validation function
    # kudos to http://iamcal.com/publish/articles/php/parsing_email/pdf/
    function is_valid_email_address($email) {
    $qtext = '[^\x0d\x22\x5c\x80-\xff]';
    $dtext = '[^\x0d\x5b-\x5d\x80-\xff]';
    $atom = '[^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c'.
    '\x3e\x40\x5b-\x5d\x7f-\xff]+';
    $quoted_pair = '\x5c\x00-\x7f';
    $domain_literal = "\x5b($dtext|$quoted_pair)*\x5d";
    $quoted_string = "\x22($qtext|$quoted_pair)*\x22";
    $domain_ref = $atom;
    $sub_domain = "($domain_ref|$domain_literal)";
    $word = "($atom|$quoted_string)";
    $domain = "$sub_domain(\x2e$sub_domain)*";
    $local_part = "$word(\x2e$word)*";
    $addr_spec = "$local_part\x40$domain";
    return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
    }

    # Init. some variables:
    $error = "";
    $success = FALSE;

    # process form if submitted:
    if(isset($_POST['submit']))
    {
    foreach($_POST as $key => $val)
    {
    if(empty($_POST[$key]))
    {
    $error .= "You must enter a value for " .
    ucwords(str_replace("_"," ",$key)) . ". ";
    }
    }
    $_POST['email_address'] = stripslashes($_POST['email_address']);
    $_POST['repeat_email'] = stripslashes($_POST['repeat_email']);
    if($_POST['email_address'] != $_POST['repeat_email'])
    {
    $error .= "Email Address is not the same as Repeat Email. ";
    }
    elseif(is_valid_email_address($_POST['email_address']) == 0)
    {
    $error .= "'{$_POST['email_address']}' does not appear to be a valid email address. ";
    }
    if(empty($error)) # no errors in input, so go ahead and email it.
    {
    $to = "$toName@$toDomain";
    $headers = "From: {$_POST['email_address']}rn".
    "Reply-to: {$_POST['email_address']}";
    if(!empty($ccName) and !empty($ccDomain))
    {
    $headers .= "rnCc: $ccName@$ccDomain";
    }
    if(!empty($bccName) and !empty($bccDomain))
    {
    $headers .= "rnBcc: $bccName@$bccDomain";
    }
    $msg = "From ".stripslashes($_POST['name'])." ({$_POST['email_address']})";
    $msg .= "nnn{$_POST['message']}";
    $result = @mail($to,
    stripslashes($_POST['subject']),
    $msg,
    $headers);
    if(!$result)
    {
    $error = "There was an unknown error while attempting to send your email.";
    }
    else
    {
    $error = "<span style='color: #930'>Thank you. Your message has been sent.</span>";
    foreach($_POST as $key => $val)
    {
    $_POST[$key] = "";
    }
    }
    }
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang='en'>
    <head>
    <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
    <title>Email Form</title>
    <style type="text/css">
    <!--
    body {
    font: 90% arial, helvetica, sans-serif;
    margin: 0;
    padding: 1em;
    background-color: silver;
    }
    #main {
    margin: 0 auto;
    padding: 1em;
    background-color: #e9f6ff;
    color: #036;
    width: 38em;
    border: inset silver 2px;
    }
    label {
    display: block;
    float: left;
    width: 8em;
    }
    h1 {
    font-size: x-large;
    margin: 5px 0;
    }
    #home {
    float: right;
    margin: 0 0 0 1em;
    }
    legend {
    font-weight: bold;
    }
    #error {
    color: #c33;
    font-weight: bold;
    margin: 0.5em 0 0 0;
    }
    fieldset p {
    margin: 0.2em 0;
    }
    #submit {
    text-align: center;
    }
    #submit input {
    font-weight: bold;
    background-color: #e9f6ff;
    color: navy;
    }
    #success {
    font-weight: bold;
    margin: 0.5em 0 0 0;
    }
    #c {
    font-size: x-small;
    text-align: center;
    }
    -->
    </style>
    </head>
    <body>
    <div id=main>
    <p id=home><a href="#">Home Page</a></p>
    <h1>Email Us</h1>
    <form action='<?php echo $_SERVER['PHP_SELF'] ?>' method=post>
    <fieldset>
    <legend>Your Contact Information</legend>
    <p><label for=name>Name:</label>
    <input type=text name=name size=30 maxlength=40<?php
    if(!empty($_POST['name']))
    {
    echo " value='{$_POST['name']}'";
    }
    ?>></p>
    <p><label for='email_address'>Email Address:</label>
    <input type=text name='email_address' size=30 maxlength=40<?php
    if(!empty($_POST['email_address']))
    {
    echo " value='{$_POST['email_address']}'";
    }
    ?>></p>
    <p><label for='repeat_email'>Repeat Email:</label>
    <input type=text name='repeat_email' size=30 maxlength=40<?php
    if(!empty($_POST['repeat_email']))
    {
    echo " value='{$_POST['repeat_email']}'";
    }
    ?>></p>
    </fieldset>
    <fieldset>
    <legend>Message</legend>
    <p><label for=subject>Subject:</label>
    <input type=text name=subject size=50 maxlength=60<?php
    if(!empty($_POST['subject']))
    {
    echo " value='". stripslashes($_POST['subject'])."'";
    }
    ?>></p>
    <p><label for=message>Message:</label><br>
    <textarea name=message cols=60 rows=8><?php
    if(!empty($_POST['message']))
    {
    echo stripslashes($_POST['message']);
    }
    ?></textarea></p>
    <p id=submit><input type=submit name=submit value="Send Email"></p>
    </fieldset>
    </form>
    <?php
    if(!empty($error))
    {
    echo "<p id=error>$error</p>n";
    }
    ?>
    </div> <!-- main -->
    <p id=c>Copyright June 2005 by <a href="http://www.charles-reace.com/">Charles
    Reace</a></p>
    </body>
    </html>
    [/code]
    ×

    Success!

    Help @crisbertoni spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.20,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...