如何将复选框值发送到电子邮件

时间:2018-07-06 作者:Shashikant Kalawatia

HTML 形式如下所示

<?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>
低于我的Javascript 密码

$(\'#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 send_test() {
    $email= $_POST[\'checkbox\'];
    echo $email;        
    wp_mail( $email, \'\'.$email.\'\');
    wp_die();
}
我是Wordpress的新手,因此如何将复选框值发送到wp\\U邮件

1 个回复
SO网友:Unnikrishnan R

首先,您需要为所有项添加一个类复选框,因为不建议对多个项使用相同的id

<input type=\'checkbox\' class="new_class" id=\'haet_mail_test_address\' name=\'ckboxs[]\'  value=\'".$emails[$i][\'user_email\']."\'>
现在,我们要更改从复选框中获取值的方式

改变

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

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

console.log(checkbox); // check if you are getting the values here
现在在“checkbox”中,您将获得所选复选框值的数组。现在可以将其作为数据传递给ajax函数

结束