I have followed two seperate Ajax tutorials online to try and make my login form not redirect to another page when there are error messages. However, neither of the methods seem to be working and my form still directs to another page and shows the error messages there. I don't understand what the problem is as I have pretty much followed the tutorials directly and it works for them but not me. Below is the code for my form:

我已经在网上跟踪了两个seperate Ajax教程,当出现错误消息时,我的登录表单不会被重定向到另一个页面。然而,这两个方法似乎都不起作用,我的表单仍然指向另一个页面并显示错误消息。我不明白问题是什么,因为我已经直接跟随了教程,它对他们有用,而不是我。以下是我的表格代码:

<form id= "login_form" action="login.php" method="post">
          <span id="login_errors"></span>
          <label>Email Address</label>
          <input type="text" name="email" id="email" required="required"/>
         <br />

         <label>Password</label>
         <input type="password" name="password" id="password" required="required"/>
         <br />

         <div class="checkbox">
         <input id="remember" type="checkbox" />
         <label for="remember">Keep me signed in</label>
         </div>

         <div class="action_btns">
         <div class="one_half last"><input type="submit" class="btn btn-blue" id="login_button" value="Login"></div>
         <div class="one_half last"><a href="#" id="register_form" class="btn">Sign up</a></div>
         </div>
</form>

and here is the ajax code based on one of the tutorials I follwed:

下面是基于我所附教程的ajax代码:

$("#login_button").click(function(){

$.post($("#login_form").attr("action"), $("#login_form:input").serializeArray(), function(info){$("#login_errors").html(info);});
});

$("login_form").submit(function(){
    return false;
});

here is the code for the other tutorial that also didn't work:

下面是另一个没有工作的教程的代码:

$("#login_button").click(function(){
var action = $("#login_form").attr("action");
var form_data = {
    email: $("#email").val(),
    password: $("#password").val(),
    is_ajax: 1
};
$.ajax({
    type:"POST",
    url:"action",
    data: form_data,
    success: function(response){
        if(response == 'success'){
            $("#login_errors").html("successful!");
        }else{
            $("#login_errors").html("unsuccessful!");   
        }

    }
});
});

I have included Jquery at the top of my webpage:

我在我的网页顶部添加了Jquery:

<script type="text/javascript" src="jquery-1.11.0.min.js"></script>

and also linked my ajax file beneath that too at the top of the page.

也在页面顶部的下方链接了我的ajax文件。

<script type="text/javascript" src="login_Ajax.js"></script>

3 个解决方案

#1


1

There is a typo in the second ajax code: the url parameter should instead of url: "action", look like this:

第二个ajax代码中有一个拼写错误:url参数应该代替url:“action”,如下所示:

url: action,

without quotes, as it refers to the variable declared a few lines above. Not sure if this solves your problem, but to see the whole picture you should also show your "login.php".

没有引号,因为它引用了上面声明的变量。不确定这是否解决了您的问题,但是要查看整个画面,您还应该显示“login.php”。

更多相关文章

  1. Jquery 只执行一次的代码
  2. BootStrap入门教程 (四)
  3. JavaScript函数中的Ruby代码
  4. Jquery禁用/启用按钮与文本框代码
  5. 推荐web 前端代码的编辑分享平台——RunJS
  6. 想要改进我的jQuery代码,做我想要的,太丑了,IMO
  7. JQuery的一些基本功能代码(CH1&2)
  8. 如何让代码等待好/取消按钮选择?
  9. 如何在angularjs代码中单元测试jquery元素

随机推荐

  1. android企业实战视频培训班
  2. android官网没法访问issue
  3. android 浏览器问题
  4. android 安卓 开发 图片库获得图片的绝对
  5. Android Studio Emulator: Process finis
  6. 黑客黑科技?Python代码让android手机实现
  7. android运行模拟器脚本(批处理)
  8. 在Eclipse中进行Android单元测试
  9. android电池信息简介
  10. Android开发之拖动条/滑动条控件、星级评