简体   繁体   English

表单即使返回假也提交

[英]Form submitting even when it is returning false

I can't seem to stop my form from submitting, even when it is shown that it is returning false. 我似乎无法阻止表单提交,即使显示表单返回的是false。 Could someone please help me spot out the error? 有人可以帮我找出错误吗?

Here is my html code: 这是我的html代码:

<form role="form" onsubmit="return login()" action="03_select_survey.html">

<!-- Username -->
<div class="form-group">
  <label for="user">Username:</label>
  <input type="text" class="form-control" id="user" placeholder="Enter Username">
</div>

<!-- Password -->
<div class="form-group">
  <label for="pwd">Password:</label>
  <input type="password" class="form-control" id="pwd" placeholder="Enter password">
</div>

<!-- Submit button -->
<button type="submit" class="btn btn-primary btn-lg">Submit</button> 

And here is my JavaScript code: 这是我的JavaScript代码:

function login() {
  "use strict";

  // Variables
  var username,
    password,
    xmlhttp,
    isFound;

  // Get username and password
  username = document.getElementById("user");
  password = document.getElementById("pwd");

  // If username or password is empty
  if (username.value === "" || password.value === "") {
    window.alert("Incorrect username or password.");
    return false;
  } else {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
      xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
      if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
        isFound = xmlhttp.responseText;
        // Check if login info exists in database
        if (isFound.value === "true") {
          return true;
        } else {
          return false;
        }
      }
    };
  }
  xmlhttp.open("GET", "login.php?user=" + username.value + "&pass=" +      password.value, true);
  xmlhttp.send();
  if (xmlhttp.onreadystatechange === true) {
      return true;
  } else {
      window.alert("Why won't you stop submitting?");
      return false;
  }
}

The code is reaching the "return false;" 该代码正在达到“返回假”; within the AJAX call, since it is alerting me with the message, but when I click OK, it proceeds to the next page instead of staying on the current page. 在AJAX调用中,由于它正在用消息提醒我,但是当我单击“确定”时,它将前进到下一页,而不是停留在当前页面上。

Show us at least the tag code as well. 至少还要向我们显示标记代码。 But the short answer it you're not stopping the form post. 但是简短的回答是您不会停止表单发布。 xmlHttp is asynchronus, so when you click your submit button, the command is fired off, but the function "login()" exits WITHOUT returning false. xmlHttp是异步的,因此当您单击提交按钮时,将触发该命令,但是函数“ login()”将退出而不会返回false。 You need to return false from that function to stop the POST. 您需要从该函数返回false以停止POST。

Here is a sample of what I would do.... 这是我会做的一个示例。

 function login() { "use strict"; // Variables var username, password, xmlhttp, isFound; // Get username and password username = document.getElementById("user"); password = document.getElementById("pwd"); // If username or password is empty if (username.value === "" || password.value === "") { window.alert("Incorrect username or password."); return false; } else { // code for IE7+, Firefox, Chrome, Opera, Safari if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new window.ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === 4 && xmlhttp.status === 200) { alert('AJAX Response.....'); isFound = xmlhttp.responseText; // Check if login info exists in database if (isFound.value === "true") { alert('We are good! Use JavaScript to navigate to new page...'); window.location = '03_select_survey.html'; return true; } else { alert('Not OK! They are not allowed in!'); return false; } } else { alert('AJAX State Change we didn\\'t expect.....'); return false; // In case we need to stop the form POST } }; } xmlhttp.open("GET", "login.php?user=" + username.value + "&pass=" + password.value, true); xmlhttp.send(); return false; // This false prevents FORM POST if we drop through before the async AJAX call finishes above } 
 <form role="form" onsubmit="return login();"> <!-- The form doesn't need action="03_select_survey.html" --> <!-- Username --> <div class="form-group"> <label for="user">Username:</label> <input type="text" class="form-control" id="user" placeholder="Enter Username"> </div> <!-- Password --> <div class="form-group"> <label for="pwd">Password:</label> <input type="password" class="form-control" id="pwd" placeholder="Enter password"> </div> <!-- Submit button --> <button type="submit" class="btn btn-primary btn-lg">Submit</button> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM