简体   繁体   中英

Contact form “Error: all fields are required”

I'm trying to make a contact form work, but i always get that error : "Error: all fields are required". I don't really see where the issue is, so any help would be great.

Thanks Mat

PHP:

<?php 
$errors = '';
$myemail = 'yo@example.com';
if(empty($_POST['Email']))
{
    $errors .= "\n Error: all fields are required";
}
$nom = $_POST['nom']; 
$prenom = $_POST['prenom']; 
$email_address = $_POST['email']; 
$sujet = $_POST['sujet']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = 'yo@example.com';
    $email_subject = "Contact form submission from Defiscalisation Andorre: $username";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Nom: $nom \n Prénom: $prenom \n Email: $email_address \n Sujet \n $sujet \n Message : \n $message"; 

    $headers = "From: $to\n"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: thanks.html');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

HTML:

            <!-- #contact-form -->
            <form id="contact-form" name="contact-form" method="post" action="contact.php">
                <div class="row-fluid">
                    <p class="span6"><input id="nom" name="nom" type="text" tabindex="1" value="" placeholder="Nom"></p>
                    <p class="span6"><input id="prenom" name="prenom" type="text" tabindex="2" value="" placeholder="Prénom"></p>
                </div>
                <div class="row-fluid">
                    <p class="span6"><input id="email" name="email" type="text" tabindex="2" value="" placeholder="E-mail"></p>
                        <p class="span6"><input id="sujet" name="sujet" type="text" tabindex="1" value="" placeholder="Sujet de votre demande"></p>
            </div>
                <div class="row">
                    <p class="span6"><textarea id="message" name="message" tabindex="4" rows="6" placeholder="Message"></textarea></p>
                </div>
                <div class="row">
                    <div class="span2">
                        <button id="sending" type="submit" class="btn btn-embossed btn-large btn-primary" data-send="Envoi...">Envoyer</button>
                    </div>
                </div>
            </form>

Javascript/jQuery:

/*===========================================================*/
/*  Contact Form
/*===========================================================*/ 
$('#contact-form').validate({
rules: {
    nom: {
        minlength: 6,
        required: true
    },
    prenom: {
        minlength: 6,
        required: true
    },
    email: {
        required: true,
        email: true
    },
    sujet: {
        minlength: 10,
        required: true
    },
    message: {
        minlength: 10,
        required: true
        }
    },
    highlight: function(element) {
        $(element).closest('.control-group').removeClass('success').addClass('error');
    },
    success: function(element) {
        element
        .text('OK!').addClass('valid')
        .closest('.control-group').removeClass('error').addClass('success');
    }
});

You cannot switch between uppercase and lowercase letters throughout your code for the same variable or element. You just need to decide on one or the other! :)

change

if(empty($_POST['Email']))

to

if(empty($_POST['email']))

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