如果同时输入了两个必填字段,则XMLHttpRequest不会为POST触发

您好,我正在尝试将用户ID和密码发送到PHP文件,并在json中回显它。如果我仅输入一个值时在输入表单标签中“要求”,它将返回带有该值的json,另一个返回为空字符串。但是,如果我同时输入两者,则根本不会进入XMLHttpRequest。 如果我删除“ required”,那么仅输入一个值也不起作用。谢谢!

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>first assignment</title>
    <link rel="stylesheet" href="style.css">
    <script>
      function curlcheck() {

        var user_id = document.getElementById("user_id").value;
        var pass = document.getElementById("password").value;

        var body = "ucid=" + user_id + "&password=" + pass;
        xhr = new XMLHttpRequest();
        xhr.open("POST", "front_curl.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        xhr.onreadystatechange = function() {

          if (xhr.status === 200 && xhr.readyState === 4) {

            var response_json = JSON.parse(xhr.responseText);
            //checks for json and "success" value from it
            console.log("in successfull responce");
            console.log(response_json.success);
            console.log(response_json);



          } else {
            //checks when status did not come back successful 
            console.log("status NOT success");
            console.log(xhr.responseText);
          }
        };

        xhr.send(body);

      }

    </script>
  </head>

  <body>
    <div class="somebox"></div>
    <ul>
      <h3>Team 1 Online Exam System</h3>
    </ul>
    <form action="" method="POST">
      <div name="form header" class="formhead">
        <h1>Welcome</h1>
        <p><span id="loginerr" class="err_alert"></span></p>
      </div>
      <div class="login_box">
        <lable for="user_id">User ID</lable>
        <input type="text" placeholder="Enter User ID" id="user_id" required>

        <label for="password">Password</label>
        <input type="password" placeholder="Enter Password" id="password" required>

        <button id="but" type="submit" onclick="curlcheck()">Login</button>

      </div>
    </form>
    <div class="footer">
    </div>

  </body>

</html>
评论