View Full Version : Multiple form fields -> e-mail
marynorn
01-17-2008, 11:42 AM
I've done this with a single form field in the e-mail body and it works fine, but once I try to add more than one form field to the e-mail, it all goes wrong. The e-mail is sent, but the only thing that shows up is the last field.
What am I doing wrong?
<?php
$to = "myemailaddy@mail.co.uk";
$subject = "Repair Request";
$body = "Tenant Name:" . $_POST['name'] . "\n";
$body = "House Number:" . $_POST['houseNumber'] . "\n";
$body = "Address Line 1:" . $_POST['address1'] . "\n";
$body = "Address Line 2:" . $_POST['address2'] . "\n";
$body = "Address Lone 3:" . $_POST['address3'] . "\n";
$body = "Postcode:" . $_POST['postcode'] . "\n";
$body = "Type of Repair:" . $_POST['optone'] . "\n";
$body = "Specific Problem Area:" . $_POST['opttwo'] . "\n";
$body = "Details of Problem:" . $_POST['details'] . "\n";
$body = "E-mail Address:" . $_POST['e-mail'] . "\n";
$body = "Daytime Telephone:" . $_POST['dayphone'] . "\n";
$body = "Evening Telephone:" . $_POST['evephone'] . "\n";
$body = "Mobile Phone:" . $_POST['mobphone'] . "\n";
$body = "Monday AM Repair" . $_POST['mondayam'] . "\n";
$body = "Tuesday AM Repair" . $_POST['tuesdayam'] . "\n";
$body = "Wednesday PM Repair" . $_POST['wednesdaypm'] . "\n";
$body = "Thursday AM Repair" . $_POST['thursdayam'] . "\n";
$body = "Friday AM Repair" . $_POST['fridayam'] . "\n";
$body = "Access Arrangements:" . $_POST['accessarrangements'];
$headers = "From: " . $_POST['e-mail'] . "\n";
mail($to,$subject,$body,$headers);
?>
boogyman
01-17-2008, 01:21 PM
the code you posted is for the one email to be sent... can you please show us how you were adding the second email?
and did you want both receipients to know you were sending to the other? or do you want it to appear to be exclusive?
marynorn
01-17-2008, 01:48 PM
It's only supposed to go to my e-mail. The person who fills out the form hits submit, which takes them to the 'thank you' page, and the form data gets e-mailed to me. I tested it out on a simple feedback form this morning, and it worked perfectly. It's just now that I'm adding more than one form field to the body of the e-mail message that I'm having the problem.
marynorn
01-17-2008, 04:36 PM
AHA! I can only use one $body value.
I got around the problem using an array:
<?php
$to = "myemailaddy@mail.co.uk" ;
$name = $_REQUEST['name'] ;
$headers = $_REQUEST['Email'] ;
$subject = "Repair Request Form";
$fields = array();
$fields{"name"} = "Tenant Name";
$fields{"houseNumber"} = "House Name";
$fields{"address1"} = "Address Line 1";
$fields{"address2"} = "Address Line 2";
$fields{"address3"} = "Address Line 3";
$fields{"postcode"} = "Postcode";
$fields{"optone"} = "Type of Repair";
$fields{"opttwo"} = "Problem";
$fields{"details"} = "Details of Problem";
$fields{"email"} = "E-mail Address";
$fields{"dayphone"} = "Daytime Telephone Number";
$fields{"evephone"} = "Evening Telephone Number";
$fields{"mobphone"} = "Mobile Telephone Number";
$fields{"mondayam"} = "Monday AM Repair";
$fields{"tuesdaypm"} = "Tuesday PM Repair";
$fields{"wednesdaypm"} = "Wednesday PM Repair";
$fields{"thursdayam"} = "Thursday AM Repair";
$fields{"fridayam"} = "Friday AM Repair";
$fields{"accessarrangements"} = "Access Arrangements";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$send = mail($to, $subject, $body, $headers);
?>
This e-mails me the info. Mostly :o
Unfortunately, my drop-down menu boxes aren't really working. I'm using the following code, anyone want to help me out here? 'optone' returns the numerical value, and 'opttwo' is blank.
<select name="optone" size="1"
onchange="setOptions(document.myform.optone.options
[document.myform.optone.selectedIndex].value);">
<option value=" " selected="selected">Select Problem Area</option>
<option value="1">Windows</option>
<option value="2">Doors</option>
<option value="3">Roof & Guttering</option>
<option value="4">Heating & Hot Water</option>
<option value="5">Electrical</option>
<option value="6">Plumbing - Bathroom</option>
<option value="7">Plumbing - Kitchen</option>
<option value="8">Kitchen</option>
<option value="9">Internal Structural</option>
<option value="10">External Areas</option>
<option value="11">Communal Areas</option>
</select>
<br>
<br>
<select name="opttwo" size="1">
<option value=" " selected="selected">Please select one of the options above first</option>
</select>
I'm not going to add in the code for 'opttwo', because it's HUGE. Imagine another 250 line of the following:
function setOptions(chosen) {
var selbox = document.myform.opttwo;
selbox.options.length = 0;
if (chosen == " ") {
selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');
}
if (chosen == "1") {
selbox.options[selbox.options.length] = new
Option('Broken Glass','broken glass');
selbox.options[selbox.options.length] = new
Option('Damaged Handle','damaged handle');
selbox.options[selbox.options.length] = new
Option('Broken Catch','broken catch');
selbox.options[selbox.options.length] = new
Option('Damaged Seal','damaged seal');
djr33
01-18-2008, 12:33 AM
You can use the array, but the easier way is to just the .= method to add to a string variable.
$i=1;$i=2;$i=3;
After that, $i will just have the value of 3.
However:
$i='a';$i.='b';$i.='c';
$i is now 'abc'.
slevin
11-07-2008, 07:04 AM
Hi,
Any chance you could tell me how to get that last thing to give out the values on new lines as opposed to having them all in a row?
would be eternally greatful
cheers,
Slevin.
Keleth
11-07-2008, 05:26 PM
Simple:
$body = "blah blah blah\n";
$body .= "blah2 blah2 blah2\n";
...
After the variable is defined, .= means to concatenate, just like how in a single string you could do
$body = 'blah'.$var1.'blah';
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.