Niel Sarvari
Niel Sarvari

Reputation: 113

Sending selected checkboxes' values to PHP

I'm building a site where the admins need to be able to select multiple database entries (clients) via checkboxes. Once the relevant clients are selected, they can click on a button that redirects them to a page where an email can now be composed for only the selected clients. My checkboxes are set up as below:

<input name = 'checked_residents' value='1' id = '1' type='checkbox' />
<input name = 'checked_residents' value='2' id = '2' type='checkbox' />
<input name = 'checked_residents' value='3' id = '3' type='checkbox' />

I then have a button (id = 'mail_selected') which, when clicked, is able to construct an array with all the id's for the selected checkboxes. See code below:

$('#mail_selected').click(function(event) {
    $(':checkbox[name="checked_residents"]:checked');
    var selectedID = [];
        $(':checkbox[name="checked_residents"]:checked').each (function () {
            var tempStr = this.id;
            selectedID.push(tempStr);
        });     
});

My problem is that I need to send the selectedID array to my php file "mailer_client.php", but I need to redirect to that page immediately, and populate my "Email To" input field with all emails from the respective ID's.

I realize that it may be difficult to understand what exactly I'm trying to do...I'm unsure myself. Please ask if anything regarding the problem is unclear.

Upvotes: 0

Views: 113

Answers (1)

If you change the name of your checkbox to be like an array, like this:

<input name = 'checked_residents[]' value='1' id = '1' type='checkbox' />
<input name = 'checked_residents[]' value='2' id = '2' type='checkbox' />
<input name = 'checked_residents[]' value='3' id = '3' type='checkbox' />

then you will receive them as an array in PHP in $_REQUEST['checked_residents'] and won't need your current code to construct the array yourself and send it.

You could then fill emails as follows:

foreach ($_REQUEST['checked_residents'] as $val) {
   $email_to .= get_email_for_id($val) . ' , ';
}

Upvotes: 1

Related Questions