HTML/PHP EMAIL FORM - FOREACH & VARIABLE VARIABLES

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChrisKNewton
Forum Newbie
Posts: 4
Joined: Sat Mar 26, 2011 11:16 pm

HTML/PHP EMAIL FORM - FOREACH & VARIABLE VARIABLES

Post by ChrisKNewton »

Hello there,

I am an intermediate PHP developer and am new to this forum. I have picked my brain for three hours on this code and still cant figure it out.

Basically, what I want this code to do is to send an email via PHP. The problem I am having is with the foreach statement and the $message5 variable in the email.

You will notice that I have a foreach statement that returns $FIELDTITLE and $VALUE into the $message5 variable... However, everytime I run the foreach statement, it obviously resets the $fieldtitle and $value to the very last submission sent through the FOREACH statement.

Basically, I want to display EVERYTHING that runs through the FOREACH STATEMENT... not just the last submission. I don't know how to display them all and still send it through the PHP mail $message5 variable. How this code works right now, it only displays the very last submission... I figure I need to incorporate a variable variable somehow so that everytime the foreach statement runs, it creates a new variable. And then $message5 can be the combination of all those variables....

Any input on how to get it t would be great!

Code: Select all

<?php

$account_name=$_POST['account_name'];
$your_name=$_POST['your_name'];
$email=$_POST['email'];
$phone_number=$_POST['phone_number'];

$month=$_POST['month'];
$day=$_POST['day'];
$year=$_POST['year'];

$date_of_order=$_POST['date_of_order'];
$need_order_by = "$month/$day/$year";

$additional_comments=$_POST['additional_comments'];

$uc_your_name = str_replace('_', ' ', ucwords($your_name));
$uc_account_name = str_replace('_', ' ', ucwords($account_name));

$message1 = "<html><body bgcolor='#666666'>Below is an order from <strong>$uc_your_name</strong> for the <strong>$uc_account_name</strong> wholesale account.<br/><br/>";
$message2 = "<strong>Order Requested On:</strong> $date_of_order<br/>";
$message3 = "<strong>Need Order Delivered By:</strong> $need_order_by<br/><br/>";
$message4 = "<strong>Order Details Below:</strong><br/><br/>";

foreach(($_POST) as $fieldtitle => $value) {

if(!empty($value) && $fieldtitle != "account_name" && $fieldtitle != "your_name" && $fieldtitle != "email" && $fieldtitle != "phone_number" && $fieldtitle != "additional_comments" && $fieldtitle != "month" && $fieldtitle != "day" && $fieldtitle != "year" && $fieldtitle != "date_of_order") {

$fieldtitle = str_replace('_', ' ', ucwords($fieldtitle));

$message5 = "<blockquote><strong> " . $fieldtitle . ":</strong> " . $value . "<br/><br/></blockquote>";
}    
}
$message6 = "<strong>Additional Comments:</strong> $additional_comments<br/></body></html>";

$to = "cknewton@ksu.edu";
$subject = "Wholesale Order From $account_name";
$from = "$email";
$headers = "From: $from\r\n";  $headers .= "Reply-To: $from\r\n"; $headers .= "MIME-Version: 1.0\r\n";  $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,"$message1 $message2 $message3 $message4 $message5 $message6", $headers);
?>
ChrisKNewton
Forum Newbie
Posts: 4
Joined: Sat Mar 26, 2011 11:16 pm

Re: HTML/PHP EMAIL FORM - FOREACH & VARIABLE VARIABLES

Post by ChrisKNewton »

What may help clarify some things with this...

I want the foreach statement to display every $fieldtitle and $value that runs through it, but when I give it a variable, such as $message5, it only takes the last defined variable (therefore the last submission through the foreach statement).

Is it possible to make this entire foreach statement a function and then return the $fieldtitle and $value as one big chunk and display them all together by calling the function through one variable, such as $message5?
ChrisKNewton
Forum Newbie
Posts: 4
Joined: Sat Mar 26, 2011 11:16 pm

Re: HTML/PHP EMAIL FORM - FOREACH & VARIABLE VARIABLES

Post by ChrisKNewton »

When I make the foreach loop a function and "return" it rather than "print" it, it still only returns on of the $fieldtitles/$values from the $_POST...
ChrisKNewton
Forum Newbie
Posts: 4
Joined: Sat Mar 26, 2011 11:16 pm

Re: HTML/PHP EMAIL FORM - FOREACH & VARIABLE VARIABLES

Post by ChrisKNewton »

Well, thanks to NO ONE for their help. My persistence yet again pays off. I figured it out.

Fixed some codes up by doing message . = "BLAH"... thanks. good game. report your loss.
Post Reply