Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
http://www.perfectionconstruction.c...
On this page, I have a form that should collect personal info and an order for fotos and submit the order to my email. When I get
the email message it lists the items selected like this:
Photo: Whatever name
Photo: Another name
Size: 8x10
Size: 8x12
(first it lists all the items in the array named "photo", then all the items in the array named "size")
What I'd rather see is this:
Photo: Whatever name
Size: 8x10
Photo: Another name
Size: 8x12
(here it would list the size WITH the name of the photo selected)
I could even live with a one-line deal, like:
Photo: Whatever name Size: 8x10
Photo: Another name Size: 8x12Here's the code for the page. I've omitted the html start and end tags and all the top code. View source if necessary. Any
advice is sincerely appreciated:
<body>
<?php$to = 'robazar@earthlink.net';
$email = $_REQUEST['email'] ;
$subject = stripslashes($_POST['subject']) . "(...Form Submittal)";
$message = '<span style="text-decoration:underline; font-weight:bold">First Name</span>' . ': ' . stripslashes($_POST['fname']). "<br />";
$address = stripslashes($_POST['email']);
$message .= '<span style="text-decoration:underline; font-weight:bold">Last Name</span>' . ': ' . stripslashes($_POST['lname']). "<br />";
$message .= '<span style="text-decoration:underline; font-weight:bold">Email</span>' . ': ' . "<ahref=\"mailto:$address\">$address<br />";
$message .= '<span style="text-decoration:underline; font-weight:bold">Message</span>' . ': ' .stripslashes($_POST['message']) . "<br />";
if (is_array($_POST['photo'])){
foreach ($_POST['photo'] as $p)
{
$message .= '<span style="text-decoration:underline; font-weight:bold">Photo</span>' . ': ' . ($p) . "<br />";
}
}if (is_array($_POST['size'])){
foreach ($_POST['size'] as $s)
{
$message .= '<span style="text-decoration:underline; font-weight:bold">Size</span>' . ': ' . ($s) . "<br />";
}
}// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $email";// IF Submit
if (isset ($_POST['submit']))
{
// Do email process and show thank you page
// But first, check the fields
if (
!empty ($_POST['email']) &&
!empty ($_POST['fname']) &&
!empty ($_POST['message'])
)
{
// Fields OK
/* Do mailing functions here */mail($to, $subject,
$message, $headers);// Show thankyou page
echo '<div class="cen">Thank you, ' . ($_POST['fname']) . '!<br />Your Info has been submitted.<br />ReturnHome
</div>' . "\n";
// Dont show the form
$dontshowform = true;
}
else
{
// Fields not OK, show notice
echo '<div class="cen">Name, Email and Message fields must be filled!</div>' . "\n";
}
}if (!isset ($dontshowform)) { ?>
<p class="big">Contact - Order Form</p>
<form method="post" action="contact_test.php">
<div class="form">
First Name: <input name="fname" type="text" />
Last Name: <input name="lname" type="text" /><br /><br />
Email: <input name="email" type="text" /><br /><br />
Subject of Message:<br /><br /><input name="subject" type="radio" value="Website Comments" />Website Comments
<br /><input name="subject" type="radio" value="Order" />Order<br /><br />
Message:<br />
<textarea name="message" rows="5" cols="40">
</textarea><br /><br />
Please select the names of the photographs you would like to order.<br />
Clicking the link to the Gallery will open that page in a new window.<br />
From Gallery 1:<br /><br />
<div>
<input type="checkbox" name="photo[ ]" value="Viga Shadows on Wall" />Viga Shadows on Wall
<input name="size[ ]" type="checkbox" value="8 x 10" />8 x 10
<input name="size[ ]" type="checkbox" value="8 x 12" />8 x 12<br /></div>
<div>
<input type="checkbox" name="photo[ ]" value="Profiles" />Profiles
<input name="size[ ]" type="checkbox" value="8 x 10" />8 x 10
<input name="size[ ]" type="checkbox" value="8 x 12" />8 x 12<br /></div><input type="checkbox" name="photo[ ]" value="Equidistant Feet" />Equidistant Feet<br />
<input type="checkbox" name="photo[ ]" value="Tomorrow's Champion" />Tomorrow's Champion<br />
<input type="checkbox" name="photo[ ]" value="Painted Neck" />Painted Neck<br />
<input type="checkbox" name="photo[ ]" value="She's Got Legs" />She's Got Legs<br />
<input type="checkbox" name="photo[ ]" value="Heaven Awaits" />Heaven Awaits<br />
<input type="checkbox" name="photo[ ]" value="Sunset through the Trees" />Sunset through the Trees<br />
<input type="checkbox" name="photo[ ]" value="Hell's Passage" />Hell's Passage<br />
<input type="checkbox" name="photo[ ]" value="Cerro Gordo Chapel" />Cerro Gordo Chapel<br />
<input type="checkbox" name="photo[ ]" value="Eyes" />Eyes<br />
<input type="checkbox" name="photo[ ]" value="Chama Heron Los Brazos Pope" />Chama Heron Los Brazos Pope<br />
<input type="checkbox" name="photo[ ]" value="Elizabeth in Color" />Elizabeth in Color<br />
<input type="checkbox" name="photo[ ]" value="Elizabeth" />Elizabeth<br />
<input type="checkbox" name="photo[ ]" value="Surrender" />Surrender<br />
<input type="checkbox" name="photo[ ]" value="In Memory of all the Mothers" />In Memory of all the Mothers<br />
<input type="checkbox" name="photo[ ]" value="Form and Function" />Form and Function<br />
<input type="checkbox" name="photo[ ]" value="8 Second Ride Series #3" />8 Second Ride Series #3<br />
<input type="checkbox" name="photo[ ]" value="Road Less Traveled" />Road Less Traveled<br />
<input type="checkbox" name="photo[ ]" value="Shady Lane" />Shady Lane<br />
<br /><br />From Gallery 2:<br /><br />
<input type="checkbox" name="photo[ ]" value="Abstract Vigas" />Abstract Vigas<br />
<input type="checkbox" name="photo[ ]" value="Barn Roof" />Barn Roof<br />
<input type="checkbox" name="photo[ ]" value="Sunny Valleys" />Sunny Valleys<br />
<input type="checkbox" name="photo[ ]" value="Reflections" />Reflections<br />
<input type="checkbox" name="photo[ ]" value="Serenity" />Serenity<br />
<input type="checkbox" name="photo[ ]" value="Nice" />Nice<br />
<input type="checkbox" name="photo[ ]" value="Chama Heron Los Brazos Pope 2" />Chama Heron Los Brazos Pope 2<br />
<input type="checkbox" name="photo[ ]" value="The Cowboy Way" />The Cowboy Way<br />
<input type="checkbox" name="photo[ ]" value="Los Brazos" />Los Brazos<br />
<input type="checkbox" name="photo[ ]" value="Sweet When They're Asleep" />Sweet When They're Asleep<br />
<input type="checkbox" name="photo[ ]" value="Kisses" />Kisses<br />
<input type="checkbox" name="photo[ ]" value="Show's Over" />Show's Over<br />
<input type="checkbox" name="photo[ ]" value="Independiente" />Independiente<br />
<input type="checkbox" name="photo[ ]" value="Window to His Soul" />Window to His Soul<br />
<input type="checkbox" name="photo[ ]" value="Independiente Too" />Independiente Too<br />
<input type="checkbox" name="photo[ ]" value="8 Second Ride Series #1" />8 Second Ride Series #1<br />
<input type="checkbox" name="photo[ ]" value="I wanna be BIG!" />I wanna be BIG!<br />
<input type="checkbox" name="photo[ ]" value="8 Second Ride Series #2" />8 Second Ride Series #2<br />
<input type="checkbox" name="photo[ ]" value="Pete" />Pete<br /><br /><input type="submit" name="submit" value="Please Submit" />
<input type="reset" value="Reset Form" />
</div>
</form><?php } ?>
</body>

Change this:
if (is_array($_POST['photo'])){
foreach ($_POST['photo'] as $p)
{
$message .= '<span style="text-decoration:underline; font-weight:bold">Photo</span>' . ': ' . ($p) . "<br />";
}
}if (is_array($_POST['size'])){
foreach ($_POST['size'] as $s)
{
$message .= '<span style="text-decoration:underline; font-weight:bold">Size</span>' . ': ' . ($s) . "<br />";
}
}To this:
if (is_array($_POST['photo']) && is_array($_POST['size'])) {
$last = (count($_POST['photo'])>count($_POST['size']))?count($_POST['size']):count($_POST['photo']);
for ($i=0; $i<$last; $i++) {
$message .= '<span style="text-decoration:underline; font-weight:bold">Photo</span>: ' . $_POST['photo'][$i] . "<br />";
$message .= '<span style="text-decoration:underline; font-weight:bold">Size</span>: ' . $_POST['size'][$i] . "<br />";
}
}
Michael J

Thanks greatly! I'll try it when I get some time, but I'm sure
it works sweet. I appreciate the help. I'm way new to this
php stuff, finding it a little tough on the brain, but the more I
ask, the clearer it gets. The only part I can't really get my
brain around yet is:
$last = (count($_POST['photo'])>count($_POST['size']))?count
($_POST['size']):count($_POST['photo']);
Do you have an English translation for that line?

I always forget the correct terminology for that (ternary operator I beleve), but it is essentialy shorthand notation for an IF/THEN statment
$x = ($a == $b) ? 'true' : 'false';
So, if $a equals $b, then $x will equal the string 'true'. Otherwise $x will equal the string 'false'
Michael J

![]() |
Can ping a url but cannot...
|
Using Apache Rewrite to f...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |