Simple PHP Email Form!

Discussion in 'PHP' started by gary15, Jun 1, 2010.

  1. #1
    Hi,

    As I'm just getting started with PHP I need a simple solution to a simple problem. I know this will probably be a easy question, but because I'm just getting started my PHP is very weak.

    Basically i need a php email form fully functional.

    At the moment I've just taken a simple email form and am using it on a website. Now my question is how to make the name and last name funtion work as well. As you can see in my HTML I have Name and Last Name, how do I get that to function with the PHP file. Or if you have a better code then the one below please let me know.

    HTML:

    “<form method='post' action='Mailform.php'>
    <p>Name: <input type='text' name='email'></p>
    <p>Last Name: <input type='text' name='email'></p>
    <p>Email: <input type='text' name='email'></p>
    <p>Subject: <input type='text' name='subject'></p>
    <p>Message:<br/>
    <textarea name='message' rows='10' cols='30'></textarea></p>
    <input type='submit' value='Submit Form'>
    </form>”

    PHP:

    “<?php
    $email_address = $_POST['email'] ;
    $subject = $_POST['subject'] ;
    $message = $_POST['message'] ;
    mail( "someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form.<br/>";
    echo "Your email has been sent.";
    ?>”

    Thanks,
     
    gary15, Jun 1, 2010 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    First change these lines in your HTML so that the 'name' attribute in the Name/Last Name input tags is unique:

    <p>Name: <input type='text' name='fname'></p>
    <p>Last Name: <input type='text' name='lname'></p>
    PHP:
    Then to use them in your .php you would reference their values with these variables:

    $_POST['fname']
    $_POST['lname']

    If you have a specific place you want to place them and are unsure, just post back with where you want them to show up in your .php.
     
    plog, Jun 1, 2010 IP
  3. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    
    <?php
    $email_address = $_POST['email'] ;
    $subject = $_POST['subject'] ;
    $message = $_POST['message'] ;
    mail( "someone@example.com", "Subject: $subject",
    $message, "From:" . $email . " );
    echo "Thank you for using our mail form.<br/>";
    echo "Your email has been sent.";
    ?>
    Code (markup):
    That should work
     
    Pudge1, Jun 1, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Where do you want the names to go ? in the sent message or in the message getting sent

    As above example use say name='fname' & name='lname' then post those.

    <form method='post' action='Mailform.php'>
    <p>First Name: <input type='text' name='fname'></p>
    <p>Last Name: <input type='text' name='lname'></p>
    <p>Email: <input type='text' name='email'></p>
    <p>Subject: <input type='text' name='subject'></p>
    <p>Message:<br/>
    <textarea name='message' rows='10' cols='30'></textarea></p>
    <input type='submit' value='Submit Form'>
    </form>
    Code (markup):
    PHP:

    <?php
    $fname = $_POST['fname']; 
    $lname = $_POST['lname'];
    
    $email_address = $_POST['email'] ;
    $subject = $_POST['subject'] ;
    $message = $_POST['message'] ;
    mail( "someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you ".$fname." ".$lname." for using our mail form.<br/>";
    echo "Your email has been sent.";
    ?>
    PHP:
    EDIT:) or just use the codes below
     
    MyVodaFone, Jun 1, 2010 IP
  5. cshwebdev

    cshwebdev Active Member

    Messages:
    226
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Since you have opted to use the post method on the form, you'll need to retrieve information using the $_POST variable.

    So, to draw your first/last name out you'd first change your name and last name's "name" to something different than email or it will always pull the last piece out (In this case the users email). Once you do that, to pull out first and last name just do something like $firstname=$_POST['firstname'] and $lastname=$_POST['lastname'], or you can concatenate them as in my example.

    __________________________________________________
    HTML:
    
    form method='post' action='Mailform.php'>
    <p>Name: <input type='text' name='firstname'></p>
    <p>Last Name: <input type='text' name='lastname'></p>
    <p>Email: <input type='text' name='email'></p>
    <p>Subject: <input type='text' name='subject'></p>
    <p>Message:<br/>
    <textarea name='message' rows='10' cols='30'></textarea></p>
    <input type='submit' value='Submit Form'>
    </form>
    
    Code (markup):
    PHP:
    
    <?php
    $email_address = $_POST['email'] ;
    $subject = $_POST['subject'] ;
    $message = $_POST['message'] ;
    $name = $_POST['firstname'] . " " . $_POST['lastname'];
    
    //We need to add the name to the message
    $message = "Contact Name: $name\r\n
                        Contact Email: $email_address\r\n\r\n" .
                       $message;
    
    mail( "someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form.<br/>";
    echo "Your email has been sent.";
    ?>
    
    Code (markup):
     
    cshwebdev, Jun 1, 2010 IP
  6. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Try this
    Add a HTML Box
    <p>Name: <input type='text' name='name'></p>
    And PHP goes like this

    
    
    <?php
    $name = $_POST['name'];
    $email_address = $_POST['email'] ;
    $subject = $_POST['subject'] ;
    $message = $_POST['message'] ;
    
    
    //We need to add the name to the message
    $message = "Contact Name: $name\r\n
                        Contact Email: $email_address\r\n\r\n" .
                       $message;
    
    mail( "someone@example.com", "Subject: $subject",
    $message, "From: $email" );
    echo "Thank you for using our mail form.<br/>";
    echo "Your email has been sent.";
    ?>
    
    
    
    PHP:
     
    roopajyothi, Jun 1, 2010 IP