2012-05-09 178 views
1

我有一个日志的形式,我试图通过AJAX提交。我有一个类似的注册表单,它工作得很好;然而,返回虚假陈述并未解雇。返回FALSE不工作的形式

我的日志形式如下:

<form name="loginForm" id="loginForm" action="userSystem/userSystem.php" method="post"> 
    <div class="loginTable"> 
     <div class="loginRow"> 
     <div class="loginCell"><label for="username">Username:</label></div> 
      <div class="loginCell"><input type="text" name=="username" id="loginFormUser"></div> 
     </div> 
     <div class="loginRow"> 
     <div class="loginCell"><label for="password">Password:</label></div> 
      <div class="loginCell"><input type="password" name="password" id="loginFormPass"></div> 
     </div> 
     <div class="loginRow"> 
     <div class="loginCell">&nbsp;</div> 
     <div class="loginCell"><input type="submit" name="submit" value="Log In" id="loginFormSubmit"></div> 
     </div> 
    </div> 
    </form> 
</section> 
<section id="loginBarRegForm"> 
    <h1>Step Two</h1> 
    <form name="regForm" id="regForm" action="usersystem/register.php" method="post"> 
    <div class="loginTable"> 
     <div class="loginRow"> 
     <div class="loginCell"><label for="r_username">Username:</label></div> 
     <div class="loginCell"><input type="text" name="r_username" id="r_username"></div> 
     <div class="loginCell"><span id="r_usernameFeedback"></span></div> 
     </div> 
     <div class="loginRow"> 
     <div class="loginCell"><label for="r_password">Password:</label></div> 
     <div class="loginCell"><input type="password" name="r_password" id="r_password"></div> 
     <div class="loginCell"><span id="r_passwordFeedback"></span></div> 
     </div> 
     <div class="loginRow"> 
     <div class="loginCell"><label for"r_vpassword">Verify Password</label></div> 
     <div class="loginCell"><input type="password" name="r_vpassword" id="r_vpassword"></div> 
     <div class="loginCell"><span id="r_vpasswordFeedback"></span></div> 
     </div> 
     <div class="loginRow"> 
     <div class="loginCell"><label for="email">Email:</label></div> 
     <div class="loginCell"><input type="text" name="r_email" id="r_email"></div> 
     <div class="loginCell"><span id="r_emailFeedback"></span></div> 
     </div> 
     <div class="loginRow"> 
     <div class="loginCell"></div> 
     <div class="loginCell"><input type="submit" name="r_submit" value="Register" id="r_submit"></div> 
     <div class="loginCell"></div> 
     </div> 
     </div> 
    </form> 

而且我想要当点击提交按钮上

$("#loginFormSubmit").click(function() { 
    form_data = { 
    username: $("#loginFormUser").val(), 
    password: $("#loginFormPass").val(), 
    operation: "login", 
    is_ajax: 1 
    } 

    $.ajax({ 
    type: "POST", 
    url: "userSystem/userSystem.php", 
    data: form_data, 
    dataType: json, 
    success: function(data) { 
     if(data.success) { 
     $("#loginBarLoginForm").fadeOut("slow"); 
     $("#userDetailsUsername").html(data.username); 
     $("#userDetailsEmail").html(data.email); 
     $("#userDetails").fadeIn('slow'); 
     } else { 
     $("#loginbarLoginForm").fadeOut("slow"); 
     $("#loginBoxResponseDiv").html(data.message); 
     $("#loginBoxResponseFrame").fadeIn("slow"); 
     } 
    } 
    }); 
    return false; 
}); 

来执行JavaScript到目前为止,这是关于最在我开发的深度应用程序中,但它只是没有意义,为什么返回false将在一个实例中工作,而不是另一个。除数据外,它们几乎是相同的功能。

谢谢你的任何帮助,你可以提供:)

编辑:更新的代码。

$("#loginFormSubmit").click(function() { 
    console.log("Click Event Firing"); 
    var form_data = { 
     username: $("#loginFormUser").val(), 
     password: $("#loginFormPass").val(), 
     operation: "login", 
     is_ajax: 1 
    }; 
    console.log("variables Assigned " + form_data['username'] + " " + form_data ['password']); 

    $.ajax({ 
     type: "POST", 
     url: "userSystem/userSystem.php", 
     data: form_data, 
     success: function(data) { 
      console.log("Ajax Working"); 
      if(data.success === true) { 
      $("#loginBarLoginForm").hide(); 
       $("#loginBoxResponseDiv").html("<p>You have been loged in " + data.username + "!"); 
       $("#loginBoxResponseFrame").fadeIn("slow"); 
      } else { 
       $("#loginBarRegForm").hide(); 
       $("#loginBoxResponseDiv").html(data.message); 
       $("#loginBoxResponseFrame").fadeIn("slow"); 
      } 
     } 
    }); 
}); 

该代码现在将返回一个JSON响应,但成功函数未被触发。

+0

尝试使用'提交()'函数,而不是点击功能。 – run

+1

如果您通过'Ajax'提交表单,那么您可以简单地使用'type =“按钮''而不是'type =”submit“'来防止表单提交 – uttam

+0

通过将提交更改为按钮,我可以得到甚至消防;但是,阿贾克斯根本不工作。 – FireCrakcer37

回答

0

事实证明,一直以来,我只是在我的解决方案中缺少一些引号。我需要更换:

dataType: json, 

有:

dataType: "json", 

一旦我得到了,该解决方案进入了地方,而现在我的用户将能够登录,而无需刷新页面。我仍然有很长的路要走这个项目,但是谢谢大家帮助我指出正确的方向。我也觉得自己能够自己找到答案;然而,如果我从SO社区那里得到了很好的帮助和提示,我从来不会这样做。你们是百万分之一!

最终的解决方案最终看上去像这样:

$("#loginFormSubmit").click(function() { 
    console.log("Click Event Firing"); 
    var form_data = { 
     username: $("#loginFormUser").val(), 
     password: $("#loginFormPass").val(), 
     operation: "login", 
     is_ajax: 1 
    }; 
    console.log("variables Assigned " + form_data['username'] + " " + form_data ['password']); 

    $.ajax({ 
     type: "POST", 
     url: "userSystem/userSystem.php", 
     dataType: "json", 
     data: form_data, 
     success: function(data) { 
      console.log("Ajax Working"); 
      console.log("data.success: " + data.success); 
      console.log("data.username: " + data.username); 
      if(data.success === true) { 
       console.log("data.success was true"); 
       $("#loginBarLoginForm").hide(); 
       $("#loginBoxResponseDiv").html("<p>You have been loged in " + data.username + "!"); 
       $("#loginBoxResponseFrame").fadeIn("slow"); 
       $("#notLoggedIn").fadeOut("slow"); 
       $("#currentlyLoggedIn").fadeIn("slow"); 
       $.cookie("username", data.username, {expires: 7, path: "/"}); 
       $.cookie("id", data.id, {expires: 7, path: "/"}); 
       $.cookie("email", data.email, {expires: 7, path: "/"}); 
       $.cookie("user_level", data.user_level, {expires: 7, path: "/"}); 
       $.cookie("active", data.active, {expires: 7, path: "/"}); 
       $.cookie("reg_date", data.reg_date, {expires: 7, path: "/"}); 
       $.cookie("last_login", data.last_login, {expires: 7, path: "/"}); 
       $("#cookieUsername").html($.cookie("username")); 
       $("#cookieID").html($.cookie("id")); 
       $("#cookieEmail").html($.cookie("email")); 
       $("#cookieUserLevel").html($.cookie("user_level")); 
       $("#cookieActive").html($.cookie("active")); 
       $("#cookieRegDate").html($.cookie("reg_date")); 
       $("#cookieLastLogin").html($.cookie("last_login")); 
      } else { 
       console.log("data.success was false"); 
       $("#loginBarRegForm").hide(); 
       $("#loginBoxResponseDiv").html(data.message); 
       $("#loginBoxResponseFrame").fadeIn("slow"); 
      } 
     } 
    }); 
}); 
3

return false的处理程序中,但ajax外以防止提交的默认操作。

$("#loginForm").on('submit',function() { //attach handler to form 
    form_data = {...}      //form data 
    $.ajax({...});      //fire ajax 
    return false;       //prevent default action 
}); 
+0

当我复制代码时,我正在尝试移动它以启动它。我已经更新了代码以反映它应该出现的位置,但我仍然得到相同的错误。我仍然在错误的地方吗? – FireCrakcer37

+0

@ FireCrakcer37会发生什么?控制台说什么?网络标签说什么? – Joseph

+0

该页面转到userSystem/userSystem.php – FireCrakcer37

0

返回假工作的需要,但问题是,成功的方法是一个回调,这是由jQuery的暗中调用每当有从Ajax调用服务器端的成功的消息,所以当时时间,你没有任何手柄,得到它的价值,所以无论你需要另一种方法来定义你的逻辑,使方法调用在这里,因为它的回调,你没有手柄来操纵。

是的,如果你想要回$.ajax()调用返回false,那么可以删除return虚假陈述的成功方法的范围之内。

0

只是一个想法,但改变输入从“提交”,“按钮”类型。

而且,只是为了调试的目的,并不动态分配click事件。改变你的JS到这一点:

function NamedFunction() { 
    form_data = { 
    username: $("#loginFormUser").val(), 
    password: $("#loginFormPass").val(), 
    operation: "login", 
    is_ajax: 1 
    } 

    $.ajax({ 
    type: "POST", 
    url: "userSystem/userSystem.php", 
    data: form_data, 
    dataType: json, 
    success: function(data) { 
     if(data.success) { 
     $("#loginBarLoginForm").fadeOut("slow"); 
     $("#userDetailsUsername").html(data.username); 
     $("#userDetailsEmail").html(data.email); 
     $("#userDetails").fadeIn('slow'); 
     } else { 
     $("#loginbarLoginForm").fadeOut("slow"); 
     $("#loginBoxResponseDiv").html(data.message); 
     $("#loginBoxResponseFrame").fadeIn("slow"); 
     } 
    } 
    }); 
    return false; 
} 

然后添加到您的提交按钮

onclick="NamedFunction(); return false;" 

我不建议你保持这种状态,但也许这些2度的变化,你真的可以追查什么发生。