Results 1 to 7 of 7

Thread: Multiple form fields -> e-mail

  1. #1
    Join Date
    Oct 2007
    Posts
    43
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Multiple form fields -> e-mail

    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?

    Code:
    <?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);
    ?>

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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?

  3. #3
    Join Date
    Oct 2007
    Posts
    43
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Oct 2007
    Posts
    43
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    AHA! I can only use one $body value.

    I got around the problem using an array:

    Code:
    <?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

    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.
    Code:
    <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 &amp; Guttering</option>
        <option value="4">Heating &amp; 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:

    Code:
    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');
    Last edited by marynorn; 01-17-2008 at 04:44 PM.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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'.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    40
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Simple:

    Code:
    $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
    Code:
    $body = 'blah'.$var1.'blah';

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •