联系表格“错误:所有字段都是必填项”

时间:2014-09-03 15:14:02

标签: php jquery post email-validation

我正在努力使联系表单工作,但我总是得到错误:“错误:所有字段都是必需的”。我真的没有看到问题出在哪里,所以任何帮助都会很棒。

由于 垫

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');
    }
});

1 个答案:

答案 0 :(得分:3)

您不能在代码中为同一个变量或元素切换大写和小写字母。你只需要决定一个或另一个! :)

更改

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

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