Im looking to validate the email text box, if email already exists, it should throw an error in span, and the text box should be empty, i did all the work with JQuery, but cant get the field cleared, please help
This is my email box
<tr>
<td>
<label> * Email</label>
</td>
<td>
<input type="text" id="email" name="email"/>
</td>
<td> <span id="email_status"></span></td>
</tr>
and my validation on blur event
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#email").blur(function() {
var email = $('#email').val();
if(email==="")
{
$("#email_status").html("");
}
else
{
$.ajax({
type: "POST",
cache: false,
url: "checkemail.php",
data: "email="+ email ,
success: function(html){
$("#email_status").html(html);
if (html == "Already exist"){
$("input:text[id=email]").val("");}
}
});
return false;
}
});
});
</script>
And finally the php part
<?php
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if(isset($_POST['email']))
{
$email=$_POST['email'];
$qu = "SELECT * FROM formdata WHERE email = '$email'";
$reslt=mysqli_query($conn,$qu);
if(mysqli_num_rows($reslt)>0)
{
echo "<span style='color:red;'>Already exist</span> ";
}
else
{
echo "<span style='color:green;'>Available</span>";
}
}
?>