How to send the checkbox value to email

HTML of form is given below

?php                             
 for($i=$start;$i  $end ;++$i)
 {                  
     if($emails[$i]!=""){                        
     $userId=$emails[$i]['ID'];
     $user_info = get_users($userId); 
     echo"tr class='iedit alternate'
     td  class='name column-name' style='border:1px solid #DBDBDB;padding-left:13px;'input type='checkbox' id='haet_mail_test_address' name='ckboxs[]'  value='".$emails[$i]['user_email']."'nbsp;".$emails[$i]['user_email']."/td";
      echo "td  class='name column-name' style='border:1px solid #DBDBDB;' ".$user_info-user_login."/td";
      echo "/tr";
     }                              
}                       
?
button class="button-primary" id="haet_mail_test_submit" name="submit" type="submit"Send Email/button
div id="haet_mail_test_sent" class="haet-mail-dialog" title="?php _e('Email sent','wp-html-mail'); ?"
p
?php _e('Your message has been sent.','wp-html-mail'); ?
/p

below my Javascript code

$('#haet_mail_test_submit').click(function(){
    var checkbox = $('#haet_mail_test_address').val();
    $.post(ajaxurl, { 'action':'haet_mail_send_test', 'checkbox':checkbox} , function(response) {
        $( "#haet_mail_test_sent" ).dialog({
            dialogClass: "no-close",
            modal: true,
            buttons: [
                {
                    text: "OK",
                    click: function() {
                        $( this ).dialog( "close" );
                    }
                }
            ]
        });
    });
});

AJAX function is given below

function send_test() {
    $email= $_POST['checkbox'];
    echo $email;        
    wp_mail( $email, ''.$email.'');
    wp_die();
}

I am new on Wordpress so how I can send the checkbox values to wp_mail

Topic wp-mail ajax Wordpress

Category Web


First you need to add a class for all checkbox as having same id for multiple items is not recommended

<input type='checkbox' class="new_class" id='haet_mail_test_address' name='ckboxs[]'  value='".$emails[$i]['user_email']."'>

Now we to change the way you are getting the values from checkbox

Change

var checkbox = $('#haet_mail_test_address').val();

To

var checkbox = $(".new_class:checkbox:checked").map(function(){
  return $(this).val();
}).get();

console.log(checkbox); // check if you are getting the values here

Now in "checkbox" you will get an array of selected checkbox values. Now you can pass it to the ajax function as data

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.