Been working on trying to figure out this bug for the last 2 hrs, and can’t figure it out.
The code works… I can send to wordpress, and see the response in the console, however it will not update check-email. Any advice will be greatly appreciated. I’ve been trial and erorring, and can’t get any progress.
HTML
<div class = "form-group container-fluid">
<label for="Registrant" class="inlineEditor">Email</label>
<div class="row ">
<div class="col" >
<input type="email" required class="form-control checking_email" name="Email" id="Email" placeholder= "[email protected]"oninput="checkemail()" style="background-color:rgb(248, 250, 251);"/>
<small id="FirstName" class="text-muted">
</small>
<div class="invalid-feedback"> Please provide a valid email address.
</div>
<span id="check-email"></span>
</div>
</div>
</div>
Ajax Script
<script>
// setTimeout(function(){ callAjax(); }, 3000);
function checkemail() {
jQuery.ajax({
url: "/wp-admin/admin-ajax.php",
data: {
'action': 'emailval',
'Email':document.getElementById('Email')
},
type: "POST",
dataType: 'json',
success:function(response){
setTimeout(function(){
jQuery("#check-email").html(response);
},3000);
},
error:function (){}
});
}
</script>
Function.php File
add_action( 'wp_ajax_emailval', 'emailval' );
add_action('wp_ajax_nopriv_emailval', 'emailval');
function emailval()
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
global $wpdb; // Use the global $wpdb object
{
error_reporting(E_ALL);
$email = $_POST['Email'];
//wp_send_json_success($email);
$emailclean = explode('@', $email);
$domain = array_pop($emailclean);
$bad_domains = array('google.ca, hotmail.com);
#$link = mysqli_connect("localhost", "root", "","site");
$sql = $wpdb->prepare("Select count(*) from " .$wpdb->prefix ."EmailValidation where email ='$email'");
//wp_send_json_success($sql);
$results = $wpdb->get_var($sql);
if($results>0)
{
echo("<span style='color:red'> It appears you are already a member, please contact us at for futher assistance. </span>");
echo "<script>$('#submit').prop('disabled',true);</script>";
}
elseif (in_array($domain, $bad_domains) ) {
echo json_encode( "<span style='color:red'> Please register with your personal email </span>");
echo "<script>$('#submit').prop('disabled',true);</script>";
echo'1';
exit();
}
else
{
echo('Thank You for becoming a member');
echo "<script>$('#submit').prop('disabled',false);</script>";
}
}
}
2
Answers
It has some problems,
in jquery code you should use : ‘Email’: document.getElementById(‘Email’).value ,
and in your ajax code you should have not use ‘echo’, it’s better to use
wp_send_json_success() or wp_send_json_error().
if you are using wp_send_json_success() this code can help you:
and if you have an empty error callback in your ajax request use this code to help more:
}
Try Out this code.