2011-09-12 29 views
0

我使用fancybox弹出了我网站上各种表单提交的模式窗口。当用户点击注册按钮时,一个模式弹出注册表单。点击提交后,信息应该通过jquery ajax调用去数据库。然而,当点击提交时,页面刷新,提交的数据作为GET发送回URL中的主页面。我不知道为什么会发生这种情况。在模式窗口中提交AJAX表单

function openModal(name, width, height){ 
     $.fancybox(
      { 
       'content'    : document.getElementById(name).innerHTML, 
       'autoDimensions'  : false, 
       'width'     : width, 
       'height'    : height, 
       'transitionIn'   : 'fade', 
       'transitionOut'  : 'fade', 
       'overlayColor'   : '#000' 
      } 
     ); 
    } 

$('.registerBtn').click(function(e) { 
     e.preventDefault(); 
     openModal('register', 450, 'auto'); 
    }); 

    $("#register_submit").click(function(e){ 
     //prevent the form from actually being submitted 
     e.preventDefault(); 

     var error = Array(); 

     // grab the user's info from the registration form 
     var username = $('input[name=register_name]'); 
     var pass = $('input[name=register_pass]'); 
     var confirm_pass = $('input[name=register_confirm_pass]'); 
     var email = $('input[name=register_email]'); 
     var confirm_email = $('input[name=register_confirm_email]'); 

     // run some validation checks and add any errors to the array 
     if(username == '' || username.length < 2) 
     { 
      error.push('Your username is too short. Minimum of 2 characters, please.'); 
     } 
     if(pass == '' || pass.length < 2) 
     { 
      error.push('Your password is too short. Minimum of 2 characters, please.'); 
     } 
     if(pass != confirm_pass) 
     { 
      error.push('The passwords you entered did not match.'); 
     } 
     if(email == '') 
     { 
      error.push('You must enter an email address.'); 
     } 
     else 
     { 
      var validEmail = checkemail(email); 
      if(validEmail == false) 
      { 
       error.push('The email address you entered is not valid.'); 
      } 
      else 
      { 
       if(email != confirm_email) 
       { 
        error.push('The email addresses you entered did not match.'); 
       } 
      } 
     } 
     // if there were no errors, pass the user info to the processing script for further validation 
     if(error.length < 1) 
     { 
      var data = 'username=' + username.val() + '&password=' + pass.val() + '&email=' + encodeURIComponent(email.val()); 
      $.ajax({ 
       url: "data.php", 
       type: "POST", 
       data: data, 
       cache: false, 
       success: function(data) { 
        // the ajax request succeeded, and the user's info was validated and added to the database 
        if(data.response == 'good') 
        { 
         $("#register_error").fadeTo(200,0.1,function() 
         { 
          $(this).html('Registering your information...').fadeTo(900,1,function(){ 
           $('#userInfo').html('<a href="index.php?mode=viewMember&u=' + data.uid + '">' + data.username + '</a>'); 
           $(this).html('You have successfully registered and logged in!<br /><a href="javascript:;" onclick="$.fancybox.close();">Close</a>'); 
          }); 
         }); 
        } 
        // ajax succeeded, but the username was in use 
        if(data.response == 'usernameBad') 
        { 
         $("#register_error").fadeTo(200,0.1,function() { 
          $(this).html('The username is already in use!').fadeTo(900,1,function(){ 
           $(this).html('Let\'s try this again...'); 
           setTimeout($.fancybox.close(), 3000); 
           openModal('register', 450, 'auto'); 
          }); 
         }); 
        } 
        // ajax succeeded but the email was in use 
        if(data.response == 'emailBad') 
        { 
         $("#register_error").fadeTo(200,0.1,function() { 
          $(this).html('The email address is already in use!').fadeTo(900,1,function(){ 
           $(this).html('Let\'s try this again...'); 
           setTimeout($.fancybox.close(), 3000); 
           openModal('register', 450, 'auto'); 
          }); 
         }); 
        } 
       } 
      }); 
     } 
     else // errors in validating in first pass 
     { 
      // iterate through the errors and fade them into the error container in the modal window 
      for(var i = 0;i < error.length;i++) 
      { 
       $("#login_error").fadeTo(400,0.1,function(){ 
        $(this).html(error[i]); 
       }); 
      } 
     } 
    }); 
+0

您需要检查脚本错误 - 如果有一个你的提交按钮单击处理程序内,则函数将返回true,浏览器将尝试提交表格。使用类似Firebug(FireFox内部)或Chrome开发工具的工具,并在点击处理程序代码中设置一个断点,以便查看发生了什么。然后发布结果。 –

+0

并确保'$(“#register_submit”)。click'处理程序正在被调用,通过在处理函数的开头放置一个警报。 – WTK

+0

看起来不像点击处理程序正在被调用。我也发布了fancybox支持小组,以查看是否有任何开发人员可以提供帮助。 – chaoskreator

回答

0

尝试这种方式结合您的表单提交:

$('#MyFormId').bind('submit', function() { 

    //ajax post here 

    return false; //Ensures the form never actually "posts" and just stays on this page 
});