【问题标题】:jQuery/ajax form - send to php problemjQuery/ajax 表单 - 发送到 php 问题
【发布时间】:2011-04-19 00:12:13
【问题描述】:

我的网站上有一个漂亮的上滑/下滑 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 的“成功”部分中回显变量。

【问题讨论】:

    标签: php jquery ajax email slidetoggle


    【解决方案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
            });
        });
    });
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        您需要使用 mysql_real_escape() 过滤帖子中的恶意代码,您可以使用正则表达式来检查有效的电子邮件。如果你用谷歌搜索它,你会发现很多关于它的文档和教程。

        您也可以轻松自在地购买(或找到免费的)即用型验证课程 -> Codecanyon validation class

        关于成功的部分看看这个问题 -> how can i create a success back function?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-11
          • 1970-01-01
          相关资源
          最近更新 更多