-1

I have tried all that I could and finally thought of asking here :

I have a checkbox where multiple values can be checked, in my php I am trying to get those values in a variable like this:

if(!empty($_POST['procedure'])) {
foreach($_POST['procedure'] as $procedure) {
      $proc = implode(",", (array)$procedure); 
}
echo $proc;
}

Issue 1. Its echoing the last value selected rather than the multiple selected.

In my html email template I have this :

<strong>Which procedure(s) are you inquiring about?</strong>
<p><?php echo $proc; ?></p>

Issue 2. Its not echoing any values here.

For the resolution of issue 1 , I tried the following code :

if(!empty($_POST['procedure'])) {
foreach($_POST['procedure'] as $procedure) {
      echo $proc; 
   }

}

Prints out the values , without comma but still empty values in email.

Also since I am emailing these values echoing the result doesnt make sense , I was trying it for testing purpose only.

Output of $proc as requested :

Vertical  Horizontal Flat

These are the values of the checkboxes , and the echo $proc is catching them correctly ..

Update: Mail function Code.

$user_email = addslashes($_POST['email']);
$filter_values = 'form, submit-btn';
$errors = '<p class="alert alert-success">Your message has been successfully sent !</p>';
$options = array(
'from_email'     =>  '',
'from_name'      =>  'form', // optional
'reply_to'       =>  'test@test.com', // optional
'adress'         =>  'my@gmail.com',
'subject'        =>  'contact',
'attachments'    =>  'img/wallacegromit.jpg', // optional
'filter_values' => 'form, submit-btn',
'html_template'  => '../forms/mailer/email-templates/contact-email.html', // optional
'css_template'   => '../forms/mailer/email-templates/contact-email.css', // optional
'sent_message'   => '<p class="alert alert-success">Your message has been successfully sent !</p>', // optional
'display_errors' => true // optional, default false
);
$sent_message = Form::sendAdvancedMail($options);

<strong>Which procedure(s) are you inquiring about?</strong>
 <p>{proc} </p>

And how in my case?

Update: This is the code which is replacing every posted values for the html email .

$filter = explode(",", $filter_values);
            for ($i = 0; $i < count($filter); $i++) {
                $filter[$i] = trim(mb_strtolower($filter[$i]));
            }
            $replacements = array_merge($values, $custom_replacements);
            foreach ($replacements as $key => $value) {
                if (!in_array(mb_strtolower($key), $filter) && !is_array($value)) {
                    $html = str_replace('{' . $key . '}', $replacements[$key], $html);

                }
            }
phpnewbie
  • 71
  • 1
  • 10

2 Answers2

0

The sentence $proc = implode(",", (array)$procedure);

Looks like does not accumulate the value in each iteration. So it could be:

  $proc = "";
  if(!empty($_POST['procedure'])) {
        foreach($_POST['procedure'] as $procedure) {
        //accumulate with '.=' operator
        $proc .= implode(",", (array)$procedure); 
  }
  echo $proc;

(Sorry I can't add comments yet, only responses)

MikeBau
  • 154
  • 1
  • 9
  • [*You should have waited...*](http://stackoverflow.com/questions/35994773/php-variable-not-showing-up-in-email#comment59642490_35994773) --- *"Still empty values in email – phpnewbie"* – Funk Forty Niner Mar 14 '16 at 18:13
  • I have changed the code adding that dot in the bigging , the values are echoing , But nothingin the html email , shows empty. – phpnewbie Mar 14 '16 at 18:14
  • Maybe you are trying to parse the template email like is specified here? [php-html-email-using-html-template](http://stackoverflow.com/questions/1739188/php-html-email-using-html-template) – MikeBau Mar 14 '16 at 18:25
  • @MikeBau if you can help me understand how I will use that with my code. – phpnewbie Mar 14 '16 at 18:37
0

echo $proc will output only the last element of array. The value of $proc will be replaced.

  $proc = "";
  if(!empty($_POST['procedure'])) {
        foreach($_POST['procedure'] as $procedure) {

        $proc = implode(",", (array)$procedure); 
  }
  echo $proc;

To solve this you have to use operator'.='

New code:

  $proc = "";
  if(!empty($_POST['procedure'])) {
        foreach($_POST['procedure'] as $procedure) {

        $proc .= implode(",", (array)$procedure); 
  }
  echo $proc;

To get the $proc in your php you should use placeholder. So change

<strong>Which procedure(s) are you inquiring about?</strong>
<p><?php echo $proc; ?></p>

to

<strong>Which procedure(s) are you inquiring about?</strong>
<p>{{proc}}</p>

In your PHP script use the following code

$gethtml = file_get_contents('../forms/mailer/email-templates/contact');
$gethtml = str_replace('{{proc}}', $proc, $gethtml);

//Send mail use

'html_template'  => $gethtml,

or use the attribute 'body' of PHPmailer

body = $gethtml;
Basti
  • 261
  • 3
  • 7
  • I am getting this warning Warning: file_get_contents($gethtml): failed to open stream: No such file or directory. – phpnewbie Mar 15 '16 at 01:28
  • since the contact-email.html is in some other directory other than main form. Directory structure is mentioned in my question. – phpnewbie Mar 15 '16 at 01:29
  • file_get_contents('../forms/mailer/email-templates/contact'); – Basti Mar 15 '16 at 04:50