Ajax Post不与表单验证工作


Ajax Post not working with Form Validation

我试着看过去的帖子,但没有看到任何可以帮助我的情况。我创建了一个ajax后验证我的网站,但由于某种原因,它不会与验证工作。如果我从验证中单独删除代码,则POST可以工作(不需要验证)。

Chrome返回"Uncaught ReferenceError: getdetails is not defined: onclick"错误,但我失去了为什么?因为当我尝试表单时,所有字段中都有有效的数据。下面是我的代码:

<input type="submit" name="submit" id="submit_btn" class="button" value="Send" onClick = "getdetails()" />

rsvp-form.js

jQuery.noConflict();
jQuery( document ).ready( function( $ ) {
    jQuery.timer = function(time,func,callback){
      var a = {timer:setTimeout(func,time),callback:null}
      if(typeof(callback) == 'function'){a.callback = callback;}
      return a;
    };
    jQuery.clearTimer = function(a){
      clearTimeout(a.timer);
      if(typeof(a.callback) == 'function'){a.callback();};
      return this;
    };

    /* RSVP Form */
    $('.error').hide();
    $('input.text-input').css({backgroundColor:"#FFFFFF"});
    $('input.text-input').focus(function(){
        $(this).css({backgroundColor:"#ffefae"});
    });
    $('input.text-input').blur(function(){
        $(this).css({backgroundColor:"#FFFFFF"});
    });
    $(".button").click(function() {
        // validate and process form
        // first hide any error messages
        $('.error').hide();
        var name = $("input#name").val();
        if (name == "") {
          $("label#name_error").show();
          $("input#name").focus();
          return false;
        }
        var email = $("input#email").val();
        if (email == "") {
          $("label#email_error").show();
          $("input#email").focus();
          return false;
        }
        var attending = $("input#attending").val();
        if (attending == "") {
          $("label#attending_error").show();
          $("input#attending").focus();
          return false;
        }
        //var dataString = 'name='+ name + '&email=' + email + '&attending=' + attending;
        //alert (dataString);return false;
        function getdetails() {
            var name = $('#name').val();
            var email = $('#email').val();
            var attending = $('#attending').val();
            $.ajax({
            type: "POST",
            url: "/wp-content/themes/Retro/sqlpost/details.php",
            data: {name:name, email:email, attending:attending}
            }).done(function( result ) {
                $('#contact_form').html("<br /><div id='message' style='display:table-cell; vertical-align:middle; text-align:center;'></div>");
                $('#message').html("Thanks for RSVP'ing " +name +",<br />we can't wait to see you!")
                //.hide()
                fadeIn(1500, function() {
                  $('#message');
                });
            })
        }
        return false;
    });
});
runOnLoad(function(){
    $("input#name").select().focus();
});

details.php

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$attending = $_POST['attending'];
mysql_connect("mkschool.db.10611925.hostedresource.com","mkschool","Correct1!");
mysql_select_db("mkschool");
$query="INSERT into students (name, email, attending) VALUES ('".$name."','".$email."','".$attending."')";
mysql_query($query) or die ('Error updating database');
//echo "Thanks for RSVP'ing " .$name. ", we can't wait to see you!"; //
?>

从提交按钮中删除onClick = "getdetails()"事件:

<input type="submit" name="submit" id="submit_btn" class="button" value="Send" />

和修改$(".button").click();的Jquery处理程序如下:

$(".button").click(function() {
    // validate and process form
    // first hide any error messages
    $('.error').hide();
    var name = $("input#name").val();
    if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
    var email = $("input#email").val();
    if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
    var attending = $("input#attending").val();
    if (attending == "") {
      $("label#attending_error").show();
      $("input#attending").focus();
      return false;
    }
    //var dataString = 'name='+ name + '&email=' + email + '&attending=' + attending;
    //alert (dataString);return false;
    var name = $('#name').val();
    var email = $('#email').val();
    var attending = $('#attending').val();
    $.ajax({
        type: "POST",
        url: "/wp-content/themes/Retro/sqlpost/details.php",
        data: {name:name, email:email, attending:attending},
        success: 
            function( result ) {
                $('#contact_form').html("<br /><div id='message' style='display:table-cell; vertical-align:middle; text-align:center;'></div>");
                $('#message').html("Thanks for RSVP'ing " +name +",<br />we can't wait to see you!")
                //.hide()
                fadeIn(1500, function() {
                  $('#message');
                });
                return false;
            }
    });
});

将输入类型type="submit"更改为type="button"

<input type="button" name="submit" id="submit_btn" class="button" value="Send" onClick = "getdetails()" />

你也可以使用Jquery库的preventDefault()函数