Get result of $_POST from multiple checkboxes on email

85 Views Asked by At

I have 1 form in with multiple checkboxes in it (each with the code):

This code displays all selected value on front page and only last selected to my email but I want to receive all the selected option on my email address.

<form action="test.php" method="post">
    <input type="checkbox" name="check_list[]" value="value 1">
    <input type="checkbox" name="check_list[]" value="value 2">
    <input type="checkbox" name="check_list[]" value="value 3">
    <input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) {
            echo $check; 
    }
}
?>
$email_message = '<p><b>Your Career Interest :</b> '.$checks .'</p>
2

There are 2 best solutions below

0
On

You can use the implode function in PHP to include all items in an array separated by a certain character like so

$checks = array('test', 'test2', 'test3');

$email_message = '<p><b>Your Career Interests: </b> '.implode(", ", $checks) .'</p>';

echo $email_message;

This prints out: Your Career Interests: test, test2, test3. I didn't go through the process of creating an HTML form and sending POST data just to give you an example, but the idea is the same.

0
On

That's because $check is already in the loop. So you need it as spare like:

<?php
if(!empty($_POST['check_list'])) {
    $checks = array();
    foreach($_POST['check_list'] as $check) {
        $checks[] = $check;
    }
    $check = implode(',', $checks);
}
?>