联系表格错误的回音

时间:2013-09-29 07:12:35

标签: javascript php contact-form

表格

<form id="contact" action="" method="post">
   <input id="name" type="text" name="name"  width="250" size="35" placeholder="Your name" required/>
     <br><br>
   <input id="email" type="text" name="email" width="250" size="35" placeholder="Your mail" required/>
   <br><br>
     <textarea id="message" name="message" rows="6" cols="40" placeholder="Your message" required></textarea>
   <br><br>
     <input type="button" value=" SEND " id="submit" />
     <input type="reset" value="Reset" name="reset">
     <div id="success"></div>
</form>  

js

$('#submit').click(function(){
        $.post("chapters/contactphp.php", $("#contact").serialize(),  function(response) {
        $('#success').html(response);
       });
      return false;
    });  

php

if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
die("All fields must be filled !");
}

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = 'qadenza@gmail.com';
$subject = 'stojada contact';
$message = 'FROM: '.$name." \nEmail: ".$email."\nMessage: \n".$message;

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 
mail($to, $subject, $message); 
echo "Thank you! Your email was sent.";
}else{
echo "The email you entered, is not valid.";
}  

所有字段都已填充,但echo为All fields must be filled

2 个答案:

答案 0 :(得分:1)

根据PHP documentation,函数filter_var返回 false 过滤后的数据

答案 1 :(得分:1)

试试这个:

<强> test.php的

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>  

<script>
    $(function() {
        $('#submit').click(function(){
                $.post("test2.php", $("#contact").serialize(),  function(response) {
                $('#success').html(response);
            });
            return false;
        });    
    });
</script>

<form id="contact" action="" method="post">
   <input id="name" type="text" name="name"  width="250" size="35" placeholder="Your name" required/>
     <br><br>
   <input id="email" type="text" name="email" width="250" size="35" placeholder="Your mail" required/>
   <br><br>
     <textarea id="message" name="message" rows="6" cols="40" placeholder="Your message" required></textarea>
   <br><br>
     <input type="button" value=" SEND " id="submit" />
     <input type="reset" value="Reset" name="reset">
     <div id="success"></div>
</form>  

<强> test2.php

<?php
if ($_POST)
{
    if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
    die("All fields must be filled !");
    }

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $to = 'qadenza@gmail.com';
    $subject = 'stojada contact';
    $message = 'FROM: '.$name." \nEmail: ".$email."\nMessage: \n".$message;

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 
    mail($to, $subject, $message); 
    echo "Thank you! Your email was sent.";
    }else{
    echo "The email you entered, is not valid.";
    }  
}
?>