0

I am trying to send each member email generate from the php if condition to an input text. The selection works well however, the problem I have is to find a way if multiple checkboxes are selected the emails sums spacing them with a coma. Right now what I have is that if I check multiple checkboxes the last email selected replaces the old one. Any Idea how Could I do this? Also if just one email is selected no coma have to be added and if the checkbox is unchecked the email is removed.

<?php if ( bp_has_members( "search_terms={$_POST['category']}")) : ?>
    <?php while ( bp_members() ) : bp_the_member(); ?>

           <table border="1" >          
            <tr>
             <td style="width:30px;">
              <input class="select_email" type="checkbox" name="get_email" value="<?php bp_member_user_email('type=user_email') ?>"/>
             </td>
             <td>
              <?php bp_member_user_email('type=user_email') ?>
             </td>
            </tr>
            </table>

    <?php endwhile; ?>
    <?php endif; ?>

Jquery

$(document).ready(function(){
    $(".select_email").click(function(){
       var select_check = $(this);
       var select_email = $(this).parent().find(".select_email").val();
       //var sum_email = select_email + ", " + select_email;
       if ( select_check.is(':checked') ) {
            // Do stuff
            alert(select_email + "   added");
            $("#my_email_to_id").val(sum_email);    
        }else {
            alert(select_email + "   removed");
        }
    });
});
SNos
  • 3,430
  • 5
  • 42
  • 92
  • I think the name of your inputs prevent you to achieve what you want i let you check [here](http://stackoverflow.com/questions/1978781/how-to-post-multiple-input-type-checkbox-as-array-in-php) for more information – JordanWD Apr 06 '15 at 16:49
  • Thanks Jordan.. Not sure how to do this with foreach.. I was trying to achieve this in js – SNos Apr 06 '15 at 17:05

1 Answers1

0
<?php if ( bp_has_members( "search_terms={$_POST['category']}")) : ?>
    <?php while ( bp_members() ) : bp_the_member(); ?>

           <table border="1" >          
            <tr>
             <td style="width:30px;">
              <input class="select_email" type="checkbox" name="get_email[]" value="<?php bp_member_user_email('type=user_email') ?>"/>
             </td>
             <td>
              <?php bp_member_user_email('type=user_email') ?>
             </td>
            </tr>
            </table>

    <?php endwhile; ?>
    <?php endif; ?>

Jquery
$(document).ready(function(){
    $(".select_email").click(function(){
       var val = $(':checkbox:checked').map(function(){ return this.value; }).toArray().join(', ');
          alert(val);
          $("#my_email_to_id").val(sum_email);
    });
});
Namitha
  • 204
  • 2
  • 6