Selecting contacts for sending mail to multiple recipients(check all check boxes on checking one checkbox and transfer their values to a textarea)
this is main checkbox to check all textboxes under it. after checking this checkbox, all sendto[] checkboxes will be checked and their values will be copied to textarea
<input type="checkbox" name="sendto[]" id="sendto[]" value="all" onClick="check(this.value)">
php code to add multiple check boxes fetched from database
<?php
$result = mysql_query("SELECT * FROM tbl_user");
while($row = mysql_fetch_array($result)) {
?>
<input type="checkbox" name="sendto[]" id="sendto[]" value="<?php echo $row['email']; ?>" onClick="addto(this)">
<?php } ?>
textarea to add multiple email ids
<textarea name="to" cols="29" rows="" id="to" required class="span121" style="width:386; height:71px;" ></textarea>
javascript function to add or remove all checkboxes' value to textarea
<script>
function check(val) {
var cnt = 5; //total number of checkboxes. can give value of php's mysql_num_rows variable
if(val=="all") {
var toval = '';
if(document.getElementsByName('sendto[]')[0].checked) {
for(i=1;i<=cnt;i++) {
document.getElementsByName('sendto[]')[i].checked=true;
toval = toval + $("#to").val() + document.getElementsByName('sendto[]')[i].value + ';';
}
$("#to").val(toval);
} else {
for(i=1;i<=cnt;i++) {
document.getElementsByName('sendto[]')[i].checked=false;
$("#to").val('');
}
}
}
}
</script>
javascript function to add or remove particular checkbox's value to textarea
<script>
function addto(objChkbx) {
var toval = $("#to").val() + objChkbx.value + ';';
if(objChkbx.checked) { $("#to").val(toval); }
else{ document.getElementById('to').value = document.getElementById('to').value.replace(objChkbx.value+';',""); }
}
</script>
Comments
Post a Comment
Please feel free to comment. I would love to hear feedback.