【问题标题】:jquery inquiry: passing message without a new pageloadjquery查询:在没有新页面加载的情况下传递消息
【发布时间】:2009-05-09 16:13:32
【问题描述】:

请原谅这个问题的尴尬,我真的不知道如何表达更好的查询......

情况:

我正在升级的 xhtml/php 页面中有一个基本的联系表格。我最近在这个页面上添加了一个新的不相关的 jquery 插件,所以如果可能的话,我想使用 jquery 功能来帮助进行一些简单的表单验证。验证已通过 PHP 在联系表单提交的单独文件中完成。

我想要发生的事情是将 PHP 验证消息传递回主(联系表单)页面,而不需要新的页面加载。我在某处看到过通过 jquery 完成此操作,但我似乎无法在这里或通过 google 再次找到它。有人可以帮忙吗?

我真的在这里寻找速度和简单性,乍一看,jquery 验证插件的大小和复杂性令人望而生畏,所以我不愿意走这条特定的路线......

【问题讨论】:

    标签: php jquery


    【解决方案1】:

    您正在寻找的就是 AJAX,它没有什么难的,尤其是 jQuery!将数据发送到 PHP 进行验证的问题在于,它会立即否定您对“速度”的要求——当 Javascript 向服务器发出请求以查看是否一切正常时,表单需要挂起。出于这个原因,通常的做法是在 服务器端和客户端进行验证。请注意,我说的是两者,因为您应该始终验证服务器上的数据。

    考虑到这一点,让我们开始吧!我将展示如何在不使用验证插件的情况下验证表单 - 对于更大的项目,您可能需要考虑它,但没有它也很容易:

    <form id='myform' action='whatever.php' method='POST'>
    First Name: <input type='text' name='first_name' class='required'><br>
    Last Name: <input type='text' name='last_name'><br>
    Email: <input type='text' name='email' class='required'><br>
    Zip: <input type='text' name='zip'><br>
    </form>
    

    如您所见,我们有名字和电子邮件以及所需的类别,所以我们可以这样做:

    $(function() { // wait for the document to be loaded
        $('#myform').submit(function() {
            var valid = true; // assume everything is okay
            // loop through each required field in this form
            $('input.required', this).each(function() {
                // if the field is empty, mark valid as false
                // and add an error class
                if($.trim($(this).val()) == '') {
                    valid = false;
                    $(this).addClass('error_input');
                }
            });
    
            // additional validation for email, zip, perhaps
    
            return valid; // if valid is true, continue form submission
        });
    });
    

    验证插件让这一切变得更整洁、更干净,但如果您只需要快速填写表单,上述方法没有任何问题。这确实很糟糕,因为您必须在两个地方设置验证规则,但除了在服务器上执行 Javascript 或调用服务器获取数据之外,如果您想快速告诉用户有问题,别无选择。

    希望这会有所帮助。

    编辑:看来我误解了你的问题。如果您想将表单数据传递给 PHP 脚本,让它验证值,并在不加载的情况下将成功或错误返回到页面,您可以执行以下操作:

    $('#myform').submit(function() {
        // allow submit if we've validated this already
        if($(this).data('done')) return true;
        var data = $(this).serialize(); // serialize the form data
        // you could get the two variables below from the actual form (DRY)
        // with $(this).attr('action'); and $(this).attr('method'); but I am
        // not sure if that is what you want...
        var action = 'validate.php'; // what script will process this
        var method = 'POST'; // type of request to make
        $.ajax({
            type: method,
            url: action, 
            data: data,
            dataType: 'json',
            success: function(d) {
                if(d.code == 'success') {
                     // everything went alright, submit
                     $(this).data('done', true).submit();
                } else {
                     alert('something is wrong!');
                }
            }
        });
        return false; // don't submit until we hear back from server   
    });
    

    然后您的 PHP 脚本所要做的就是返回类似这样的内容,使用 json_encode 来指示一切是否正常:

    // verify everything...
    // data will be in $_POST just like a normal request
    if($everythingOK) {
        $code = 'success';
    } else {
        $code = 'error';
    }
    print json_encode(array('code' => $code));
    exit;
    

    我还没有测试过上面的 Javascript,但我很确定它应该是正确的。我希望有所帮助。 :)

    【讨论】:

    • 感谢您的回复!它很有趣也很有用,但并不完全是我的想法。我知道我的问题还有很多不足之处......澄清:我提到的“速度”是我自己的代码实现速度(浅学习曲线)而不是页面处理速度:-)我只想要服务器端 php 生成的消息(成功或错误)在没有新页面加载的情况下传递回提交页面。不是一个新的单独的验证程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多