I have a step form. I need to send email if i click next button. It works only if i click 2nd step next button. Here is my jquery:
let ajaxurl = 'admin-ajax.php';
let i=0;
jQuery( ".btn-next" ).each(function() {
jQuery(this).on("click", function(){
if(i === 0){
console.log('First Button');
jQuery.post(ajaxurl, {action: 'send_mail'});
}
else if(i === 1){
console.log('Second Button');
jQuery.post(ajaxurl, {action: 'send_mail'});
}
else{
console.log('Third Button');
jQuery.post(ajaxurl, {action: 'send_mail'});
}
i++;
});
});
PHP functions:
function send_mail() {
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM my_table");
$step_complete = $results[0]->step_completed;
$response = $results[0]->response;
$response2 = json_decode($response);
if(!empty($results)){
//echo $step_complete;
if($step_complete==1){
wp_mail('test@mail.com', 'First Step', 'First Step Complete');
}
if($step_complete==2){
wp_mail('test@mail.com', 'Second Step', 'Second Step Complete');
}
}
}
add_action('wp_ajax_send_mail', 'send_mail');
add_action('wp_ajax_nopriv_send_mail', 'send_mail');
It only send email when i click 2nd step next button( that means if second time function load it will work) then it will send step_complete == 1 conditions mail.
I need it will works every conditions properly.