Question

i have some understanding difficulties. i have some checkboxes in html like this:

<input type="checkbox" name="a[]" value="1">
<input type="checkbox" name="a[]" value="2">
<input type="checkbox" name="a[]" value="3">

i like to evaluate multiple choices of a[] to see what was selected thats why i have to use an array.

okay, now the problem is: after submit i have posted that array.

if (isset($_POST['submit'])) {

    $a = $_POST['a'];
}

further i like to set some message into another array for each value that is selected:

if ($a === '1'){
   $msg[] = "text1";
}
if ($a === '2'){
   $msg[] = "text2";
} and so on...

now i have stored these messages to the array $msg[]

the next step and here comes my real problem:

i like to display the whole selection in a mail that will be send. so up to that point i have:

$to = "a@b.cd";
$subject = "some text";
$message = "some text... 

show what was selected:

$msg

end text";...

so normally i know that i have to use foreach like:

<?php if(isset($msg)):?>
    <?php foreach($msg as $m):?> 
        <p>
        <?php echo $m;?>
        </p>
    <?php endforeach;?>
<?php endif;?>

to make it visible. my problem is to implement this into that message from the mail into that quotation marks.

so if there is someone who could help me out, i really would appreciate.

thanks a lot.

Was it helpful?

Solution

Simply use string concatenation, which in PHP is done using the . (dot) operator.

$message = "some text... \n\nshow what was selected:\n\n";

foreach ($a as $b)
{
    $message .= $b . "\n";
}

$message .= "end text";

Furthermore you should look into your variable naming ($a isn't really descriptive), and $a is an array, so you can't do if ($a === '1'), you should use in_array().

OTHER TIPS

You should do

$msg = array();
foreach($a as $checked){
    $msg[$a] = "text$a";
}

and then

<?php if(isset($msg)):?>
    <?php foreach($msg as $k => $m):?> 
        <p>
        <?php echo "$k : $m";>
        </p>
    <?php endforeach;?>
<?php endif;?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top