I do know that this has been ceaselessly requested right here, however not one of the options I discover appear to work for me.
The problem is as follows: at any time when I put up the shape, I’ve a console log with the response which is a full html web page moderately than a easy message.
Backend script:
add_action( 'admin_ajax_nopriv_email_verification_form', 'verify_and_sanitize_email_form' );
add_action( 'admin_ajax_email_verification_form', 'verify_and_sanitize_email_form' );
// E mail verification callback
perform verify_and_sanitize_email_form() {
// Examine referer
check_ajax_referer( '9pU0Pk7T01', 'safety' );
if(empty($_POST) || !isset($_POST['rguroo_email']) || !isset($_POST['rguroo_email_confirmation']) || !isset($_POST['rguroo_desired_action'])) {
echo 'There's a number of empty fields';
wp_die();
}
$sanitized_email = sanitize_email( esc_html($_POST['rguroo_email'] ));
$sanitized_email_confirmation = sanitize_email( esc_html($_POST['rguroo_email_confirmation'] ));
$desired_action = esc_html($_POST['rguroo_desired_action']);
if(!is_email( $sanitized_email ) || !is_email( $sanitized_email_confirmation )) {
echo 'E mail shouldn't be legitimate';
wp_die();
}
if($sanitized_email !== $sanitized_email_confirmation) {
echo 'Emails don't match';
wp_die();
}
if($desired_action !== 'buy' || $desired_action !== 'renewal' || $desired_action !== 'trial') {
echo 'Deadly error with radio buttons';
wp_die();
}
if(!isset($_COOKIE['rguroo_form_type'])) {
echo 'Server error';
wp_die();
}
// pupil electronic mail verification logic
$form_type = $_COOKIE['rguroo_form_type'];
if($form_type === 'pupil') {
$path = substr($sanitized_email, -4);
if($path !== '.edu') {
echo 'Not a legitimate pupil electronic mail';
wp_die();
}
// Different college particular logic right here
}
setcookie('rguroo_form_action',$desired_action, 14 * DAY_IN_SECONDS);
setcookie('rguroo_form_email', $sanitized_email, 14 * DAY_IN_SECONDS);
echo "success";
wp_die();
}
Frontend javascript:
jQuery(doc).prepared(perform () {
jQuery('type#email-verification').on( 'submit', perform () {
var form_data = jQuery(this).serializeArray();
form_data.push( { "identify" : "safety", "worth" : ajax_info.ajax_nonce } );
jQuery.ajax({
url : ajax_info.ajax_url,
sort : 'put up',
knowledge : form_data,
success : perform( response ) {
console.log(response);
if(response !== 'success') {
jQuery('.error').innerHTML = response;
} else {
location.reload();
}
},
fail : perform( err ) {
jQuery('.error').innerHTML = "Can not contact server";
}
});
return false;
});
});
Type (utilized in a shortcode):
perform output_email_verification() {
return '<type motion="'.esc_url( admin_url("admin-ajax.php") ).'" technique="put up" id="email-verification">
<p class="error">'.$error.'</p>
<enter sort="radio" label="Buy 12 months entry" worth="buy" identify="rguroo_desired_action" checked>Buy 12 months entry</enter>
<enter sort="radio" label="Renew account" identify="rguroo_desired_action" worth="renewal">Renew account</enter>
<enter sort="radio" label="Create trial account" identify="rguroo_desired_action" worth="trial">Create trial account</enter>
<p class="form-subtext">* signifies required area</p>
<p>E mail tackle*</p>
<enter sort="textual content" identify="rguroo_email" required>
<p>Re-type electronic mail tackle*</p>
<enter sort="textual content" identify="rguroo_email_confirmation" required>
<enter sort="hidden" identify="motion" worth="email_verification_form">
<enter sort="submit" worth="Submit">
</type>';
}
Issues I do know: there is not a problem with jQuery sending the put up (or at the least making the put up request). On submit jQuery sends the put up with all the appropriate parameters. I additionally know that I am not utilizing totally different nonces. I’ve tried inserting error_logs() throughout the callback perform, however none of them ever acquired displayed in debug.log. This leads me to imagine that the callback is rarely truly known as, however I do not know why. Additionally, even when I give the shape a take a look at which ought to fail, jQuery nonetheless reads it as successful.
I’ve wasted all day on this manner and really feel like an fool. Might somebody with a couple of extra braincells than me please inform me the place I am going flawed?
Thanks a lot.