繁体   English   中英

jQuery / ajax表单-发送到php问题

[英]jQuery/ajax form - send to php problem

我的网站上有一个外观很好的slideup / slidedown 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的“成功”部分中回显变量,因此无法使用它。

$(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
        });
    });
});

您需要使用mysql_real_escape()过滤帖子中的恶意代码,并且可以使用正则表达式检查有效的电子邮件。 如果您使用google进行搜索,则会发现很多与此有关的文档和教程。

您还可以使自己变得容易,并购买(或找到一个免费的)现成的验证类-> Codecanyon验证类

关于成功部分,请看这个问题-> 如何创建成功后退功能?

http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

基于JQuery的验证脚本,在代码的一部分验证失败时,可以很好地验证并自动插入错误消息。 只需包含文件,添加jquery(有关最佳方法,请参见示例源),然后将“ required”元素添加到类名中。 应该是您问题的完美解决方案。...无需疯狂的数学运算或单独的选择器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM