Multiple Email recipients with check boxes
I have a list of recipients with email links. Can a check box be used to select specific names and have those addresses populate in the To: field on a blank email?
I have a list of recipients with email links. Can a check box be used to select specific names and have those addresses populate in the To: field on a blank email?
onniec77729686 wrote
But sadly, I don't know how to get there. :-(
You would need to use some server-side language to process the information, which IF you ARE dealing with form information you should be doing anyway.
It's really quite easy. The below example is in it's simplest form. You loop through ALL the ticked checkboxes and send the email message to their designated email addresses, collected from the forms input/checkbox fields.
<?php
if(isset($_POST['submit'])) {
foreach ($_POST['email'] as $email) {
mail($email, 'Subject','Message Goes Here', "From: Whoever");
}
}
?>
<h1>Send Email</h1>
<form name="send_email" action="index.php" method="post">
<p><input type="checkbox" name="email[]" value="email@address_1.com">email@address_1.com</p>
<p><input type="checkbox" name="email[]" value="email@address_2.com">email@address_2.com</p>
<input type="submit" name="submit" value="Submit" />
</form>
Breaking the above down (you should set the form 'action' field to the page which processes the script. I very often tend to keep the php script in the same file as the form itself, right at the top of the file, before anything else:
If you keep the script in the same page as the form you don't want the script to run when the page loads so you surround the script in a php 'if' query, testing to see if the form submit button has been clicked. If it has the script runs.
if(isset($_POST['submit'])) {
}
You then use a 'foreach' loop to loop through all the form checkboxes that have been checked:
foreach ($_POST['email'] as $email) {
}
Then mail your message to the specific emails using the php mail function:
mail($email, 'Subject','Message Goes Here', "From: Whoever");
You collect the email form field values into an array when the checkbox/s have been checked so the script can loop through them. That is why you have set the name field to 'email[]' - the brackets designates to store in an array of information:
<p><input type="checkbox" name="email[]" value="email@address_1.com">email@address_1.com</p>
That might help, a bit, maybe.
There are other more complicated workflows like using ajax to smooth the process of page reload but at this stage I think this will be enough for you to try and get your head around.
It looks more complex than it really is and it goes without saying you should always use validation on your forms. This is just a bare-bones example of the process without the all singing all dancing necessities in place. You can choose as many or as little of those as you like.
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.