user3189160
user3189160

Reputation: 23

how to get multiple checkbox value through $_POST via email php

My html form for checkbox

<form  method="POST" class="registrationForm" id="registrationForm" action="processmail.php">
   <input type="checkbox" name="Activity[]" value="value1"  /> value1 <br>
    <input type="checkbox" name="Activity[]" value="value2"  /> value2<br>
    <input type="checkbox" name="Activity[]" value="value3"  /> value3 <br>

    <input type="submit" id="sendMessage" name="sendMessage" value="Submit" />
</form>

My processmail.php

$message .= "<table border='1'>";
$message .= "<tr><td>Field of Activity    </td>.  <td>".$_POST['Activity']."</td></tr>";
$message .= "</table>";

by using above syntax, it only shows "Array" as options for my Field of Activity field, plz help me get options separated with comma which are selected....

Upvotes: 2

Views: 750

Answers (5)

SSpoke
SSpoke

Reputation: 5836

Try this to figure it out.

<?php print_r($_POST); ?>

or even if you don't know if its a POST or a GET.

<?php print_r($_REQUEST); ?>

Anyways it's a array I guess you could use foreach()

foreach($_POST['Activity'] as $value) {
    echo "value is $value";
}

Upvotes: 0

Sadat
Sadat

Reputation: 3501

$message .= "<tr><td>Field of Activity    </td>.  <td>".$_POST['Activity']."</td></tr>";

TO

$message .= "<tr><td>Field of Activity    </td>.  <td>".implode(',',$_POST['Activity'])."</td></tr>";

Upvotes: 0

Joel Cox
Joel Cox

Reputation: 3469

use PHP implode function:

http://php.net/function.implode

Replace the line

$message .= "<tr><td>Field of Activity    </td>.  <td>".$_POST['Activity']."</td></tr>";

with

$message .= "<tr><td>Field of Activity    </td>.  <td>".implode(',', $_POST['Activity'])."</td></tr>";

Also - to prevent potentially malicious users injecting html into your page by editing the checkbox values before submitting, use htmlentities as well to escape the data before you re-render it onto the page

$message .= "<tr><td>Field of Activity    </td>.  <td>".implode(',', array_map('htmlentities', $_POST['Activity']))."</td></tr>";

Upvotes: 0

Floris
Floris

Reputation: 46365

The selections are in an array - one element for each option selected. You can use a foreach to loop over these elements and display them.

Upvotes: 0

Pupil
Pupil

Reputation: 23948

You need to loop through the values of the checkbox.

Right now, its getting an array instead of string.

Therefore, it printing as Array.

You can do it so by:

<?php
$ActivityStr = '';
if (isset($_POST['checkboxName']) && count($_POST['checkboxName']) > 0) {
    $i=0;
    foreach ($_POST['checkboxName'] as $cbox) {
        if ($i>0){
            $ActivityStr .= ',';
        }
        $ActivityStr .= $cbox;
        ++$i;
    }
}
$message .= ""; $message .= "Field of Activity ".$ActivityStr."";
$message .= "";
?>

Upvotes: 2

Related Questions