-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'       =>  '[email protected]', // optional
'adress'         =>  '[email protected]',
'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);

                }
            }
16
  • Okie, Why in the email its empty? any thought. Commented Mar 14, 2016 at 18:05
  • the email code is a seperate template file in a folder. All other values are visible except this one. Commented Mar 14, 2016 at 18:07
  • trying your suggestions.. now Commented Mar 14, 2016 at 18:08
  • @Don'tPanic I have changed the code to this - if(!empty($_POST['procedure'])) { foreach($_POST['procedure'] as $procedure) { $proc .= implode(",", (array)$procedure); echo $proc; } } Still empty values in email Commented Mar 14, 2016 at 18:11
  • got my money on the (unknown) form and the rest of the unshown PHP including mail(). Commented Mar 14, 2016 at 18:15

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

You should have waited... --- "Still empty values in email – phpnewbie"
I have changed the code adding that dot in the bigging , the values are echoing , But nothingin the html email , shows empty.
Maybe you are trying to parse the template email like is specified here? php-html-email-using-html-template
@MikeBau if you can help me understand how I will use that with my code.
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;

3 Comments

I am getting this warning Warning: file_get_contents($gethtml): failed to open stream: No such file or directory.
since the contact-email.html is in some other directory other than main form. Directory structure is mentioned in my question.
file_get_contents('../forms/mailer/email-templates/contact');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.