javascript 检查数据库中是否存在电子邮件文本框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27501132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:33:55  来源:igfitidea点击:

Check whether the email textbox value exists in database

javascriptphpjquery

提问by saru

I want to check the value in text box is present in database and clear the box if the value exists in the database without refreshing the whole page.

我想检查文本框中的值是否存在于数据库中,如果该值存在于数据库中而不刷新整个页面,则清除该框。

HTML:

HTML:

<p>Email<input type="text" name="email" id="email" size=18 maxlength=50 required></p>

Query:

询问:

$echeck="select email from register where email='$email'";
$echk=mysql_query($echeck);
$ecount=mysql_num_rows($echk);
if($ecount!=0)
{
    echo ("<SCRIPT LANGUAGE='JavaScript'>
    var asign=document.getElementById(email);   //now show null value
    asign.value="";                
    </SCRIPT>");
}

If I use alert it will refresh the page.

如果我使用警报,它将刷新页面。

window.alert('Email Id already exist');

回答by Suchit kumar

try to implement something like this:

尝试实现这样的事情:

<script type="text/javascript">
function checkMailStatus(){
    //alert("came");
var email=$("#email").val();// value in field email
$.ajax({
    type:'post',
        url:'checkMail.php',// put your real file name 
        data:{email: email},
        success:function(msg){
        alert(msg); // your message will come here.     
        }
 });
}

</script>

  <p>Email<input type="text" name="email" id="email" onblur="checkMailStatus()"size=18 maxlength=50 required></p>

your php: checkMail.php

你的 php: checkMail.php

$echeck="select email from register where email=".$_POST['email'];
   $echk=mysql_query($echeck);
   $ecount=mysql_num_rows($echk);
  if($ecount!=0)
   {
      echo "Email already exists";
   }

回答by Razorphyn

I'm not sure because I have always used JQuery, however I try:
Source: W3Schools

HTML file:

我不确定,因为我一直使用 JQuery,但是我尝试:
来源:W3Schools

HTML 文件

<script type="text/javascript">
function checkInput(){
            var email=document.getElementById('email').value.trim();
            if(email.length==0)
                 return;
            var xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=function(){
              if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    if(xmlhttp.responseText==0){
                        document.getElementById('email').value='';
                        alert('Email already exists');
                    }
                else if(xmlhttp.responseText==1){
                      document.getElementById('email').value='';
                      alert('Invalid Email');      
                }
            }
            xmlhttp.open("GET","check.php?email="+email,true);
            xmlhttp.send();

        }

</script>
//I would use check mail button, to prevent accindental unfocus
<p>Email<input type="text" name="email" id="email" onblur="javascript:checkInput();" size=18 maxlength=50 required></p>

check.php

检查.php

//This is a simple input check
//The way you check the value isn't secure, you should sanitaze the input
// consider PDO or MySQLi

//Check if email otherwise stop php and clean input
if(!filter_var($_GET['email'], FILTER_VALIDATE_EMAIL)){
    echo '1';
    exit();
}

$echeck="select email from register where email='$_GET['email']'";
$echk=mysql_query($echeck);
$ecount=mysql_num_rows($echk);
if($ecount!=0){
    echo '0';
    exit();
}
else{
    echo '2';
}