如何使用Jquery发送数据

时间:2013-07-17 06:33:46

标签: jquery html forms validation send

我刚刚完成了我的表单验证,我使用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 个答案:

答案 0 :(得分: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;
                }

答案 1 :(得分:0)

AJAX可以将数据发送到某个地址,(如果您想将数据发送到SQL数据库或其他内容,则为PHP文件)

$("#buttonID").click(function(){ $.ajax({ url: "<something.com/file.php";, data: {username: $(username).val()}, type: "post", success: function(msg){ }); });

答案 2 :(得分:0)

老兄,你没有'提交按钮'并且缺少一些表格属性,至少表格标签应该是这样的:

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

操作应该是服务器脚本的URL

答案 3 :(得分:0)

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

将数据发布到地址