Well, Newcomer, did you actually look at the code I gave you and study how it works? The solution is there.
First, you MUST post your code inside the PHP tags. It is annoying that you just place it in your post as a
text list. It is harder for us to use the code that way. Please make sure for future posts that you place the
code inside of the PHP tags. You just have to press the PHP button above the text area when you are doing
your post and place your code inside of those PHP tags. Thank you for doing this from here on… Oh, also,
if you have two or more files you are posting, place each inside separate PHP tags so we can copy them as
separate files.
Now, you have posted code. It pulls the list[] array. In my sample code, it just displays each that was in
the array. In your code you want to place that info into a message that is emailed. Here is your code now
in a PHP tags. In this way, the code has numbers that we can reference for our discussions…
Your code all in one file:
[php]
<?php
$your_email ='
[email protected]';// <<=== update to your email address
session_start();
$errors = '';
$name = '';
$email = '';
$subject = '';
$user_message = '';
$value = '';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$user_message = $_POST['message'];
$value = $_POST['list'];
///------------Do Validations-------------
if(empty($name) || empty($email))
{
$errors .= "\n Name and Email are required fields. ";
}
if(empty($_POST['list'])) {
echo "Select at least one option. ";
} else {
// All checkboxes used a name of "list[]" and that is the array of your check boxes
echo "You previously selected:
";
foreach ($_POST['list'] as $value) {
echo $value . "
";
}
}
if(IsInjected($email))
{
$errors .= "\n Bad email value!";
}
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
//Note: the captcha code is compared case insensitively.
//if you want case sensitive match, update the check above to
// strcmp()
$errors .= "\n The captcha code does not match!";
}
if(empty($errors))
{
//send the email
$to = $your_email;
$subject="Customer Information";
$from = $your_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A user $name submitted the Contact Form:\n".
"Name: $name\n".
"Email: $email \n".
"Subject : $subject\n".
"Message: \n ".
"$user_message\n".
"List: $value\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to, $subject, $body,$headers);
header('Location: thanks-contact.html');
}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
<?php
if(!empty($errors)){
echo "
".nl2br($errors)."
";
}
?>
Name:
Email:
Subject:
Comments:
<?php echo htmlentities($user_message) ?>
What are you looking for?:
Web Design
Web Hosting
Logo Design
Web Application
Web Matainace
Javascript
[/php]
So, as you see in line # 66 you have this: “List: $value\n”.
This is where you pull ONE value from your variable $value and put it into your message. So, your code is
doing what you told it to do. It does not LOOP thru the list[] array and display all of the items inside it, it
just displays the last item that you stored into the $value variable…
There are two ways to fix this. You can place a loop inside the area where you create the message to pull
out all of the values or, you can do that ahead of time in a separate code area and create a string that holds
all of your list[] array contents… Either way should work. Here is one way to do it. Replace the section
where you are creating the $body of your message with this:
[php]
$body = “A user $name submitted the Contact Form:\n”.
“Name: $name\n”.
“Email: $email \n”.
“Subject : $subject\n”.
"Message: \n ".
//---------------------------------------------------
"List: ";
foreach ($_POST[“list”] as $value) {
$body .= $value . “\n”;
}
//---------------------------------------------------
$body .= “$user_message\n”.
“List: $value\n”.
“IP: $ip\n”;
[/php]
I did not test this, but, it should work. Basically, I replaced where you placed one value from the list[] array
into your $body of the message and placed all of them there. I put a blank line above them and below so
they stand out, not needed in your live code… Note that you can use a string such as $body and add to it
as needed. the " .= " means to concatenate or add-to-end-of-string. So, my small change in your code
adds all of the list[] entries and then continues with the rest of your creation of the $body message.
Hope that makes sense to you… Good luck!