How could I process this html email form with php?

Discussion in 'PHP' started by Anhalo, Oct 24, 2009.

  1. #1
    Hey,

    I've been looking into email forms recently, and making them from scratch...

    How would I use php to submit the following html form to an email address?

    <html> 
    <title>Contact Us</title>
    <form method="post" action="#">
    First name: <input type="text" size="22" maxlength="12" name="firstname"><br />
    <p>
    Last name: <input type="text" size="22" maxlength="36" name="lastname"><br />
    <p>
    Select project type:
    <select name="type">
    <option value="Web_design">Web design</option>
    <option value="Graphic_design">Graphic design</option>
    <option value="Branding">Branding</option></select><br />
    <p>
    Your message:
    <br>
    <textarea rows="5" cols="27" name="message" wrap="physical"></textarea><br />
    <p>
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
    
    &nbsp; &nbsp;<input type="submit" value="Submit" name="submit">
    <input type="reset" value="Reset" name="reset"><br />
    </form><br />
    </html>
    Code (markup):
    Many thanks,

    Anhalo.
     
    Anhalo, Oct 24, 2009 IP
  2. Gediminas

    Gediminas Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    //get data from form...
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $type=$_POST['type'];
    $message=$_POST['message'];
    
    $subject=$first_name . ' ' . $lastname . ' - ' . $type;
    //and lets send a mail...
    if(mail('your@mail.com',$subject,$message,'From: my mebsite')) {
       echo 'E-Mail was sent.';
    }
    else {
      echo 'Something wrong';
    }
    ?>
    PHP:
    Hope it works!

    P.S. save it as php file and change form tag's action attribute to php file path. If you didn't know :)
     
    Last edited: Oct 24, 2009
    Gediminas, Oct 24, 2009 IP
  3. Anhalo

    Anhalo Active Member

    Messages:
    454
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thank you very much, that worked perfect! :)
     
    Anhalo, Oct 24, 2009 IP
  4. MrPJH

    MrPJH Well-Known Member

    Messages:
    1,066
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    155
    #4
     
    MrPJH, Oct 25, 2009 IP