3

请我尝试通过使用 Ajax 同时向我的数据库提交和验证我的表单,但它对我不起作用。这是我的jQuery

$(document).ready(function(){
       $(".button").click(function(){
             $("#myform").validate();

            //Ajax to process the form
       $.ajax({
           type: "POST",
           url: "process.php",
           data: { firstname: $("#firstname").val()},
           success: function(){
                               $('#message').html(data);
                               }
               });
          return false;
       });
      });

问题是当我提交表单时,Ajax 表单提交给自己。请问一起使用jquery validate和$ .ajax的正确方法是什么?

4

4 回答 4

5

data在成功函数中作为参数传递:

success: function(data){

你的success函数不会做任何事情,因为你还没有定义data

于 2013-07-08T13:23:22.000 回答
3

试试这个(按预期为我工作):

HTML 表单:

<link rel="stylesheet" href="http://jquery.bassistance.de/validate/demo/css/screen.css" />
<script src="http://jquery.bassistance.de/validate/lib/jquery.js"></script>
<script src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script>    
// JQuery Script to submit Form
$(document).ready(function () {
    $("#commentForm").validate({
        submitHandler : function () {
            // your function if, validate is success
            $.ajax({
                type : "POST",
                url : "process.php",
                data : $('#commentForm').serialize(),
                success : function (data) {
                    $('#message').html(data);
                }
            });
        }
    });
});
</script>

<form class="cmxform" id="commentForm" method="get" action="">
    <fieldset>        
        <p>
            <label for="cname">Name (required, at least 2 characters)</label>
            <input id="cname" name="name" minlength="2" type="text" required />
            <p>
                <label for="cemail">E-Mail (required)</label>
                <input id="cemail" type="email" name="email" required />
            </p>
            <p>
                <label for="curl">URL (optional)</label>
                <input id="curl" type="url" name="url" />
            </p>
            <p>
                <label for="ccomment">Your comment (required)</label>
                <textarea id="ccomment" name="comment" required></textarea>
            </p>
            <p>
                <input class="submit" type="submit" value="Submit" />
            </p>
    </fieldset>
</form>

<div id="message"></div>

PHP代码:

<?php
echo $_POST['email'];
?>
于 2013-07-08T13:31:27.207 回答
3

您忘记传递响应

$(document).ready(function() {
    $(".button").click(function() {

        //check the validation like this
        if ($("#myform").valid()) {
            //Ajax to process the form
            $.ajax({
                type: "POST",
                url: "process.php",
                data: {
                    firstname: $("#firstname").val()
                },

                //you forget to passs the response
                success: function(response) {
                    $('#message').html(response);
                }
            });
            return false;
        }
    });
});
于 2013-07-08T13:25:34.137 回答
0

首先,如果验证不通过,为什么要提交表单?试试这个,如果 validate 真的验证:

$(function(){
    $(".button").click(function(){
        var myform = $("#myform");
        if (myform.validate()) {        
            $.post("process.php", myform.serialize(), function(data){
                $('#message').html(data);
            });            
        }
        return false;
    });     
});
于 2013-07-08T13:28:04.367 回答