ram
ram

Reputation: 21

Get result of $_POST from multiple checkboxes on email

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>

Upvotes: 1

Views: 77

Answers (2)

FSodic
FSodic

Reputation: 73

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);
}
?>

Upvotes: 3

Fady Bark
Fady Bark

Reputation: 105

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.

Upvotes: 2

Related Questions