0

形式

<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.

4

2 回答 2

1

根据PHP文档,该函数filter_var返回false过滤后的数据。

于 2013-09-29T07:24:55.913 回答
1

尝试这个:

测试.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.";
    }  
}
?>
于 2013-09-29T09:01:26.253 回答