我的网站上有一个漂亮的幻灯片/滑动jquery表单。它将数据发送到同一文件以发送电子邮件。这很好用:
$(document).ready(function(){
$('div.contact a.submit').click(function() {
var name = $('div.contact input.name').val();
var email = $('div.contact input.email').val();
var message = $('div.contact textarea.message').val();
$('div.contact').slideUp('slow', function() {
$.ajax({
type: "POST",
url: "test.php",
data: "name=" + name + "&email=" + email + "&message=" + message,
success: function(msg)
{
$('div.contact').html('<h1>Contact</h1><div class="comment">Success!</div><div class="clear"></div><p class="indent">Thank you, we will be in contact shortly.</p>');
$('div.contact').slideDown('slow');
}//end of success
});//end of ajax
});
});
});
在test.php顶部的php发送电子邮件:
include("expander-b1.0.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
sendmail("admin@site.co.uk", $message, "Contact message from $name", $email);
这是从外部文件获取我的简单邮件功能。但是,我想要一些方法来验证表单条目(包括电子邮件验证),然后在联系人潜水中显示错误。我对jQuery或ajax没有经验,但是无法让我在php中使用if语句并回应ajax“success”部分中的变量。
答案 0 :(得分:1)
$(document).ready(function(){
$('div.contact a.submit').click(function() {
var name = $('div.contact input.name').val();
var email = $('div.contact input.email').val();
var message = $('div.contact textarea.message').val();
//Email Validation
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(email) == false) {
alert('Invalid Email Address');
return false;
}
//Name and message VALIDATION
//-------- goes here ------------//
$('div.contact').slideUp('slow', function() {
$.ajax({
type: "POST",
url: "test.php",
data: "name=" + name + "&email=" + email + "&message=" + message,
success: function(msg)
{
$('div.contact').html('<h1>Contact</h1><div class="comment">Success!</div><div class="clear"></div><p class="indent">Thank you, we will be in contact shortly.</p>');
$('div.contact').slideDown('slow');
}//end of success
});//end of ajax
});
});
});
答案 1 :(得分:0)
你需要使用mysql_real_escape()过滤帖子中的恶意代码,你可以使用正则表达式来检查有效的电子邮件。如果你谷歌为它,你会发现很多关于它的文档和教程。
你也可以轻松自己购买(或找到一个免费的)即用型验证类 - &gt; Codecanyon validation class
关于成功部分看看这个问题 - &gt; how can i create a success back function?
答案 2 :(得分:0)
http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/
基于JQuery的验证脚本,如果代码的某一部分验证失败,它可以很好地验证并自动插入错误消息。只需包含文件,添加jquery(参见最佳方法的示例源)并在类名中添加“required”元素。应该是您问题的完美解决方案....不需要疯狂的数学或个人选择器。