Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onreadystatechange不提交表单或始终提交表单_Javascript_Php_Jquery_Html_Forms - Fatal编程技术网

Javascript onreadystatechange不提交表单或始终提交表单

Javascript onreadystatechange不提交表单或始终提交表单,javascript,php,jquery,html,forms,Javascript,Php,Jquery,Html,Forms,我想在注册页面上检查我输入的电子邮件地址。我现在使用onreadystatechange从数据库中获取带有responseText的响应。如果responseText为true,则不要提交表单,否则提交。 但是,即使条件为真,我的代码也将始终提交我的表单 //////////////HTML文件////////////////// <form action="regist.php" method="post" id="submitForm"> <label>*Emai

我想在注册页面上检查我输入的电子邮件地址。我现在使用onreadystatechange从数据库中获取带有responseText的响应。如果responseText为true,则不要提交表单,否则提交。 但是,即使条件为真,我的代码也将始终提交我的表单

//////////////HTML文件//////////////////

<form action="regist.php" method="post" id="submitForm">
  <label>*Email:</label>
  <label class="validate" id="emailError">XXXX@XXXX.XXX</label></br>
  <input type="text" id="email" name="email"/></br>
  <label>*First Name:</label>
  <label class="validate" id="firstnameError">Letters only!</label>
  <input type="text" id="firstname" name="firstname"/></br>
  <label>Last Name:</label>
  <input type="text" id="lastname" name="lastname"/></br>
  <label>*Password:</label>
  <label class="validate" id="passwordError">at least 6 characters!</label>
  <input type="password" id="password" name="password"/></br>
  <label class="validate" id="used">Email has been used!</label></br>
  <button id="Validate">Register</button>
</form>
$(document).ready(function () {
  $("#regi").click(function () {
    var popup = $("#register").dialog({modal: true});
    popup.show();
  });
  $("#Validate").click(function () {
    validate();
  });
});

function validate() {
  var email = document.getElementById("email").value;
  var error = $("#used");
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET", "backend.php?req=checkDuplicate&user=" + email, true);
  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      if (xmlhttp.responseText == 1) {//check if email existed in db, return 1
        error.show();
        console.log("find match! Do not proceed!!");
        $('#submitForm').submit(false);
      }
      else {
        error.hide();
        console.log("Redirect");
        $('#submitForm').submit(true);//only here should do redirect
      }
    }
  }
  xmlhttp.send();
}

首先,您应该阅读一些有关的文档

功能验证{ var email=document.getElementByIdemail.value; var错误=$used; var xmlhttp=新的XMLHttpRequest; xmlhttp.openGET,backend.php?req=checkDuplicate&user=+email,true; xmlhttp.onreadystatechange=函数{ 如果xmlhttp.readyState==4&&xmlhttp.status==200{ 如果xmlhttp.responseText==1{//检查数据库中是否存在电子邮件,返回1 error.show; console.logfind匹配!不要继续!!; //如果你不想继续,就不要叫提交! //$'submitForm'。submitfalse; } 否则{ error.hide; console.logRedirect; //$'submitForm'.submittrue;//只有在这里才应该执行重定向 //使用jQuery来触发submit,删除真值 $'submitForm'。提交; } } } xmlhttp.send; } 使用此选项可以获得与以下相同的结果:

功能验证{ var email=$email.val; var错误=$used; var url=backend.php?req=checkDuplicate&user=+电子邮件; $.geturl,函数数据{ ifdata==1{ error.show; console.logfind匹配!不要继续!!; }否则{ error.hide; console.logRedirect; $'submitForm'。提交 } }; }
我希望这将对您有所帮助。

您似乎已经使用了jQuery,那么为什么不使用jQuery ajax方法呢?谢谢。我以后再试试。谢谢你的回答。我尝试了你推荐的方法,但仍然有问题。然后,我删除了html中的表单标记和js中的.submit方法。相反,我使用了xmlhttp.openGET,backend.php?req=checkDuplicate&user=+email+&last=+lastname+&first=+firstname+&pass=+password,true;将值作为解决方法传递。现在一切都好了。