简体   繁体   English

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

[英]Check whether the email textbox value exists in database

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');

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";
   }

I'm not sure because I have always used JQuery, however I try: 我不确定,因为我一直使用JQuery,但是我尝试:
Source: W3Schools 资料来源: W3Schools

HTML file : 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 check.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';
}

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

相关问题 为什么 sendSignInLinkToEmail(email, settings) 不检查给定的 email 是否存在? - Why does sendSignInLinkToEmail(email, settings) not check whether the given email exists? 验证以检查文本框值是否已更改 - Validation to check whether the textbox value is changed or not 检查 JSON 对象中是否存在值 - Check whether a value exists in JSON object jQuery AJAX检查数据库中是否存在电子邮件不工作 - jQuery AJAX check if email exists in database not working 检查数组的每个对象中是否存在键值对 - Check whether key value pair exists in every object of array 检查Javascript中的数组中是否存在属性值为“x”的对象 - Check whether an object with property value 'x' exists in an array in Javascript Vanilla JS+AJAX - 检查一个值是否已经存在 - Vanilla JS+AJAX - check whether a value already exists 如何在jQuery验证程序中使用AJAX检查值是否已存在? - How to check whether a value already exists using AJAX in jQuery validator? 我怎样才能建立一个系统来检查我在文本框中写的文本是否存在于工作表中? - How can I make a system that will check whether the text I write in the textbox exists in the sheets? 使用angularjs在TextBox中键入内容时,检查JavaScript对象中是否存在值 - Check if a value exists in an JavaScript object as you type in TextBox using angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM