表格
<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
。
答案 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.";
}
}
?>