0

我刚刚验证了我的表单,我找到了一个使用 jquery 的教程。唯一缺少的是将数据发送到适当的地址。我怎么做?

这是我的表单 HTML 代码示例

<form id="register-form">

         <div class="four columns alpha contact-form">

         <label>prefix</label>
         <input type="text"  />
         <label><span class="asterisk">*</span>first name</label>
         <input type="text" name="firstname"/>
         <label><span class="asterisk">*</span>middle name</label>
         <input type="text" name="middlename"/>
         <label><span class="asterisk">*</span>last name</label>
         <input type="text" name="lastname"/>
         <label><span class="asterisk">*</span>email address</label>
         <input type="text" name="email"/>

        </div>
     </form>

这是用于验证我的表单中的数据的 Jquery。

/* 基本 jQuery 验证表单演示代码 版权所有 Sam Deering 2012 许可证:http ://www.jquery4u.com/license/ */

(function($,W,D)
{
var JQUERY4U = {};

JQUERY4U.UTIL =
{
    setupFormValidation: function()
    {
        //form validation rules
        $("#register-form").validate({
            rules: {
                firstname: "required",
                middlename: "required",
                lastname: "required",
                email: {
                    required: true,
                    email: true
                },
                zip: "required",
                city: "required",
                subject: "required",
                message: "required",

                agree: "required"
            },
            messages: {
                firstname: "Please enter your first name",
                middlename: "Please enter your middle name",
                lastname: "Please enter your last name",
                email: "Please enter a valid email address",
                zip: "Please enter your zip code",
                city: "Please state your city",
                subject: "Please state your inquiry",
                message:"Please include a short message"

            },
            submitHandler: function(form) {
                form.submit();
            }
        });
    }
}

//when the dom has loaded setup form validation rules
$(D).ready(function($) {
    JQUERY4U.UTIL.setupFormValidation();
});

})(jQuery, window, document);

我还不擅长 jquery,目前我还在学习。我读过我需要使用这个 .serialize() 来获取数据。不确定我是否正确

4

4 回答 4

1

是的,您可以像这样使用serialize()通过ajax将表单数据发送到您的脚本

$.ajax({
         type: "POST",
         url: "yourScript.php",
         data: $('#register-form').serialize(),
         success: function(html){
            //do something
         }
       });

你可以在你的submitHandler函数里面写

submitHandler: function(form) {
                    $.ajax({
                      type: "POST",
                      url: "yourScript.php",
                      data: $('#register-form').serialize(),
                      success: function(html){
                        //do something
                      }
                    });
                    return false;
                }
于 2013-07-17T06:42:22.777 回答
0

伙计,您没有“提交按钮”,并且缺少一些表单属性,至少表单标签应该是这样的:

<form id="register-form" name="register-form" action="" method='post'>

action 应该是服务器脚本的 URL

于 2013-07-17T06:38:09.720 回答
0

要将数据发送到适当的地址,您必须使用普通发布或使用ajax将数据发布到地址

于 2013-07-17T06:44:37.693 回答
0

AJAX 可以将数据发送到某个地址,(一个 PHP 文件,以防您想将其发送到 SQL 数据库或其他东西)

$("#buttonID").click(function(){ $.ajax({ url: "<something.com/file.php";, data: {username: $(username).val()}, type: "post", success: function(msg){ }); });
于 2013-07-17T06:37:09.953 回答