Page 1 of 1

html form array to email

Posted: Tue Jan 10, 2006 9:54 pm
by endorphine44
twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have a html form built with a multiple select field, user selects 1, 2 or 3 options from a list then fills other fields. When submitted a php script processes and emails me the information and displays a quick thank you note.
However, when I recieve the email it only shows the last selected course, so if someone selects multiple courses I only see the last one in my email. I know its the way I have the sendmail.php written but i don't know how to fix it. I'm new at this so any help is appreciated.



[b]
html form:[/b]

Code: Select all

<Form action="sendmail.php" method="POST">
<p><strong>Select course(s):</strong><br /><select name="course[]" multiple>
<option value="Course 1">Course 1</option>
<option value="Course 2">Course 2</option>
<option value="Course 3">Course 3</option></select>
<p><strong>Name:</strong><br /> <input type="text" size="20" name="name" /></p>
<p><strong>Email address:</strong><br /><input type="text" size="20" name="email" /></p>
<p><strong>Phone Number:</strong><br /><input type="text" size="20" name="phone" /></p>
<p><strong>City:</strong><br /><input type="text" size="20" name="city" /></p>
<p><strong>Comments:</strong><br /><textarea name="comments" cols=30 rows=5></textarea></p>
<p><input type="submit" value="send" /><input type="reset" value="reset form" /></p>
</Form>


sendmail.php:

Code: Select all

<?php
echo "<p>Thank you, <b>$_POST[name]</b>. </p>";
echo "<p>We will contact you at <b>$_POST[email]</b> as soon as possible.</p>";
if(!empty($_POST[course])){echo"<ul>";
	foreach ($_POST[course] as $value) 
	{echo "<li>$value";}
	echo "</ul>";
	}
$msg = "Course: $_POST[course]\n";
$msg .= "Name: $_POST[name]\n";
$msg .= "Email: $_POST[email]\n";
$msg .= "Phone Number: $_POST[phone]\n";
$msg .= "City: $_POST[city]\n";
$msg .= "Comments: $_POST[comments]\n";

$recipient = "example@domain.com";
$subject = "email subject";
$mailheaders = "MIME-Version: 1.0\n";
$mailheaders = "Content-type: text/html; charset=ISO-8859-1\n";
$mailheaders = "From: Contact Page";
$mailheaders .= "Reply-To: $_POST[email]";

mail($recipient, $subject, $msg, $mailheaders);
?>

twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Jan 10, 2006 9:58 pm
by spamyboy
use "[quote]" and "

Code: Select all

"

Posted: Wed Jan 11, 2006 4:49 am
by mickd
or better yet, "[ code ]" and "[ /code ]" too but without the spaces...

Posted: Wed Jan 11, 2006 7:05 am
by sheila
Look at these lines

Code: Select all

if(!empty($_POST[course])){echo"<ul>";
    foreach ($_POST[course] as $value)
    {echo "<li>$value";}
    echo "</ul>";
    }
$msg = "Course: $_POST[course]\n";
You need to capture the list of courses and add it to the email msg.

Code: Select all

$courses = '';
if(!empty($_POST[course]))
{
    echo"<ul>";
    foreach ($_POST[course] as $value)
    {
        echo "<li>$value";
        // note the course for the email message
        $courses .= "Course: $value\n";
    }
   echo "</ul>";
}
$msg = $courses;

Thanks

Posted: Wed Jan 11, 2006 7:13 am
by endorphine44
Ok that makes sense, don't know why I couldn't think of that last night. I'll try that tonight when I get home. Thanks for the help.

Posted: Wed Jan 11, 2006 9:47 am
by Skittlewidth
Another note:

Writing

Code: Select all

$_POST[course]

is wrong.

It should be:

Code: Select all

$_POST['course']
Read the link to find out why:
http://www.php.net/manual/en/language.t ... rray.donts