PHP form and adding more $_REQUEST's

Discussion in 'PHP' started by lithman, Aug 14, 2007.

  1. #1
    I set up a form that action="contact.php"

    I have this in my contact.php file
    
    <?php
    $to = "my@email.org";
    $subject = "Registration";
    $email = $_REQUEST['email'] ;
    $message = $_REQUEST['message'] ;
    $headers = "From: $email";
    $sent = mail($to, $subject, $message, $headers) ;
    if($sent)
    {print "Your mail was sent successfully"; }
    else
    {print "We encountered an error sending your mail"; }
    ?>
    
    Code (markup):
    The form works fins if my only form fields are Email and Message. However, I have First Name, Last Name, country, city and a bunch of other fields people need to fill out.

    How do I add more lines to the php code above so it puts all the other fields in the email body? I tried this below but it didn't work??

    $message = $_REQUEST['firstname'] ;
    $message = $_REQUEST['lastname'] ;
    $message = $_REQUEST['city'] ;

    firstname, lastname, city being the input="name" in the form.
     
    lithman, Aug 14, 2007 IP
  2. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Replace the = with .= like as follows:

    $message .= $_REQUEST['firstname'] ;
    $message .= $_REQUEST['lastname'] ;
    $message .= $_REQUEST['city'] ;
    Code (markup):
    If you'd like new lines for each extra bit:

    $message .= "\n".$_REQUEST['firstname'] ;
    $message .= "\n".$_REQUEST['lastname'] ;
    $message .= "\n".$_REQUEST['city'] ;
    Code (markup):
     
    norfstar, Aug 14, 2007 IP
  3. lithman

    lithman Well-Known Member

    Messages:
    1,189
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Ok, I did that and all the content showed up in the email. But in the email body, all the entries came back in 1 line of text like this

    'Test 7DavidLithmanJacksonville'

    Instead of

    Test 7
    David
    Lithman
    Jacksonville

    Anyway to fix that?
     
    lithman, Aug 14, 2007 IP
  4. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #4
    try this:
    
    $message .= "\r\n".$_REQUEST['firstname'] ;
    $message .= "\r\n".$_REQUEST['lastname'] ;
    $message .= "\r\n".$_REQUEST['city'] ;
    
    Code (markup):
     
    frankcow, Aug 14, 2007 IP
    lithman likes this.
  5. lithman

    lithman Well-Known Member

    Messages:
    1,189
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Thanks frankcow! Works great.

    One last question though...

    Anyway to include the 'firstname', 'lastname', 'city' in the email body? so It would read

    firstname: David
    lastname: Lithman
    city: Jacksonville

    If not, it's ok. This should work!
     
    lithman, Aug 14, 2007 IP
  6. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try:

    $message .= "\r\n".'firstname: '.$_REQUEST['firstname'] ;
    $message .= "\r\n".'lastname: '.$_REQUEST['lastname'] ;
    $message .= "\r\n".'city: '.$_REQUEST['city'] ;
    Code (markup):
     
    norfstar, Aug 14, 2007 IP
    lithman likes this.
  7. lithman

    lithman Well-Known Member

    Messages:
    1,189
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Worked great! Thanks!!! rep for everyone
     
    lithman, Aug 14, 2007 IP
  8. MykeXero

    MykeXero Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I can do all that above and more... just with 2 lines.... even if you decided to add more options to submit in the form this will list those as well... just 2 lines!
    I`d replace the 3 lines with $message with this:

    
    foreach ($_REQUEST as $key => $value)
             $message .= "\r\n". "$key: $value";
    
    Code (markup):
    Now you can post 3349329423949234 more options if you wanted to, and they`d be listed in the email. All you have to do is change the html on the form with more fields and no more touching the php.

    Further Note: Since this is user input sensitive, i`d probably add an array of values to check for security reasons. (as oppose to accepting all $_REQUEST values), but if you are lazy the code above works.
     
    MykeXero, Aug 14, 2007 IP