[英]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.