PHP Contact Form

Discussion in 'PHP' started by bradleykirwan, May 7, 2010.

  1. #1
    Hi there,

    I have an email form and I need the contents of the message to be the output of the page.

    Here is my code:

    <?php
    $to = "name@ips.com";
    $subject = "HTML email";
    
    $message = "";
    
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    
    // More headers
    $headers .= "From: sender@isp.com\r\n" .
         "X-Mailer: php";
    
    
    mail($to,$subject,$message,$headers);
    
    ?>
    
    
    <html>
    <table border="1">
    <tr><td colspan="2"><b>Enquiry Details</b></td></tr>
    <tr><td width="140">Arrival Date:</td><td width="360"><?php echo $_POST["date_day"]; ?>/<?php echo $_POST["date_month"]; ?>/<?php echo $_POST["date_year"]; ?></td></tr>
    <tr><td>Departure Date:</td><td><?php echo $_POST["dateto_day"]; ?>/<?php echo $_POST["dateto_month"]; ?>/<?php echo $_POST["dateto_year"]; ?></td></tr>
    <tr><td>Rooms required:</td><td><?php echo $_POST["single"]; ?> Single Room(s), <?php echo $_POST["double"]; ?> Double Room(s), <?php echo $_POST["twin"]; ?> Twin Room(s)</td></tr>
    <tr><td>Party Size:</td><td><?php echo $_POST["adults"]; ?> Adult(s), <?php echo $_POST["children"]; ?> Children</td></tr>
    <tr><td>Age(s) of Children:</td><td><?php echo $_POST["childrenage"]; ?></td></tr>
    <tr><td>Message:</td><td><?php echo nl2br($_POST[message]) ?></td></tr>
    <tr><td colspan="2"><b>Enquiry Details</b></td></tr>
    <tr><td>First Name:</td><td><?php echo $_POST["firstname"]; ?></td></tr>
    <tr><td>Last Name:</td><td><?php echo $_POST["lastname"]; ?></td></tr>
    <tr><td>Email:</td><td><?php echo $_POST["email"]; ?></td></tr>
    <tr><td>Phone:</td><td><?php echo $_POST["phone"]; ?></td></tr>
    <tr><td>Fax:</td><td><?php echo $_POST["fax"]; ?></td></tr>
    <tr><td>Address:</td><td><?php echo $_POST["addressline1"]; ?><br />
    		<?php echo $_POST["addresline2"]; ?></td></tr>
    <tr><td>City:</td><td><?php echo $_POST["city"]; ?></td></tr>
    <tr><td>Country:</td><td><?php echo $_POST["country"]; ?></td></tr>
    </table>
    </html>";
    PHP:
    So I need all the HTML code at the bottom to be in the $message.

    How do I do this because what I have tried so far only results in fails.

    Thanks in advance!
     
    bradleykirwan, May 7, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    use concat equals


    $message = "line 1";
    $message .= "line 2";
    $message .= "line 3";

    etc......

    echo $message;
     
    bartolay13, May 7, 2010 IP
  3. bradleykirwan

    bradleykirwan Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So I used:

    <?php
    $to = "name@ips.com";
    $subject = "HTML email";
    
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    
    // More headers
    $headers .= "From: sender@isp.com\r\n" .
         "X-Mailer: php";
    
    
    mail($to,$subject,$message,$headers);
    
    
    
    $message = "<html>";
    $message .= "<table border="1">";
    $message .= "<tr><td colspan="2"><b>Enquiry Details</b></td></tr>";
    $message .= "<tr><td width="140">Arrival Date:</td><td width="360"><?php echo $_POST["date_day"]; ?>/<?php echo $_POST["date_month"]; ?>/<?php echo $_POST["date_year"]; ?></td></tr>";
    $message .= "<tr><td>Departure Date:</td><td><?php echo $_POST["dateto_day"]; ?>/<?php echo $_POST["dateto_month"]; ?>/<?php echo $_POST["dateto_year"]; ?></td></tr>";
    $message .= "<tr><td>Rooms required:</td><td><?php echo $_POST["single"]; ?> Single Room(s), <?php echo $_POST["double"]; ?> Double Room(s), <?php echo $_POST["twin"]; ?> Twin Room(s)</td></tr>";
    $message .= "<tr><td>Party Size:</td><td><?php echo $_POST["adults"]; ?> Adult(s), <?php echo $_POST["children"]; ?> Children</td></tr>";
    $message .= "<tr><td>Age(s) of Children:</td><td><?php echo $_POST["childrenage"]; ?></td></tr>";
    $message .= "<tr><td>Message:</td><td><?php echo nl2br($_POST[message]) ?></td></tr>";
    $message .= "<tr><td colspan="2"><b>Enquiry Details</b></td></tr>";
    $message .= "<tr><td>First Name:</td><td><?php echo $_POST["firstname"]; ?></td></tr>";
    $message .= "<tr><td>Last Name:</td><td><?php echo $_POST["lastname"]; ?></td></tr>";
    $message .= "<tr><td>Email:</td><td><?php echo $_POST["email"]; ?></td></tr>";
    $message .= "<tr><td>Phone:</td><td><?php echo $_POST["phone"]; ?></td></tr>";
    $message .= "<tr><td>Fax:</td><td><?php echo $_POST["fax"]; ?></td></tr>";
    $message .= "<tr><td>Address:</td><td><?php echo $_POST["addressline1"]; ?><br />";
    	$message .= "	<?php echo $_POST["addresline2"]; ?></td></tr>";
    $message .= "<tr><td>City:</td><td><?php echo $_POST["city"]; ?></td></tr>";
    $message .= "<tr><td>Country:</td><td><?php echo $_POST["country"]; ?></td></tr>";
    $message .= "</table>";
    $message .= "</html>";
    
    ?>
    PHP:
    And I got:
    Parse error: syntax error, unexpected T_LNUMBER in process.php on line 19
     
    bradleykirwan, May 7, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    You might need to turn off Notices if you get any

    
    <?php
    $to = "name@ips.com";
    $subject = "HTML email";
    
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    
    // More headers
    $headers .= "From: sender@isp.com\r\n" .
         "X-Mailer: php";
    
    
    mail($to,$subject,$message,$headers);
    
    
    
    $message = "<html>";
    $message .= "<table border=\"1\">";
    $message .= "<tr><td colspan=\"2\"><b>Enquiry Details</b></td></tr>";
    $message .= "<tr><td width=\"140\">Arrival Date:</td><td width=\"360\"><?php echo $_POST[date_day]; ?>/<?php echo $_POST[date_month]; ?>/<?php echo $_POST[date_year]; ?></td></tr>";
    $message .= "<tr><td>Departure Date:</td><td><?php echo $_POST[dateto_day]; ?>/<?php echo $_POST[dateto_month]; ?>/<?php echo $_POST[dateto_year]; ?></td></tr>";
    $message .= "<tr><td>Rooms required:</td><td><?php echo $_POST[single]; ?> Single Room(s), <?php echo $_POST[double]; ?> Double Room(s), <?php echo $_POST[twin]; ?> Twin Room(s)</td></tr>";
    $message .= "<tr><td>Party Size:</td><td><?php echo $_POST[adults]; ?> Adult(s), <?php echo $_POST[children]; ?> Children</td></tr>";
    $message .= "<tr><td>Age(s) of Children:</td><td><?php echo $_POST[childrenage]; ?></td></tr>";
    $message .= "<tr><td>Message:</td><td><?php echo nl2br($_POST[message]) ?></td></tr>";
    $message .= "<tr><td colspan=\"2\"><b>Enquiry Details</b></td></tr>";
    $message .= "<tr><td>First Name:</td><td><?php echo $_POST[firstname]; ?></td></tr>";
    $message .= "<tr><td>Last Name:</td><td><?php echo $_POST[lastname]; ?></td></tr>";
    $message .= "<tr><td>Email:</td><td><?php echo $_POST[email]; ?></td></tr>";
    $message .= "<tr><td>Phone:</td><td><?php echo $_POST[phone]; ?></td></tr>";
    $message .= "<tr><td>Fax:</td><td><?php echo $_POST[fax]; ?></td></tr>";
    $message .= "<tr><td>Address:</td><td><?php echo $_POST[addressline1]; ?><br />";
        $message .= "   <?php echo $_POST[addresline2]; ?></td></tr>";
    $message .= "<tr><td>City:</td><td><?php echo $_POST[city]; ?></td></tr>";
    $message .= "<tr><td>Country:</td><td><?php echo $_POST[country]; ?></td></tr>";
    $message .= "</table>";
    $message .= "</html>";
    
    ?>
    
    PHP:
     
    MyVodaFone, May 7, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    @MyVodafone

    You should'nt include php tags within a concatenated string (especially in this case), its bad coding, dirty and unefficient, use output buffering as its not only easier but more effective.

    <?php
    $to = "name@ips.com";
    $subject = "HTML email";
    
    $message = "";
    
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    
    // More headers
    $headers .= "From: sender@isp.com\r\n" .
         "X-Mailer: php";
    
    ob_start();
    $_POST = array_map('strip_tags', $_POST);
    ?>
    
    
    <html>
    <table border="1">
    <tr><td colspan="2"><b>Enquiry Details</b></td></tr>
    <tr><td width="140">Arrival Date:</td><td width="360"><?php echo $_POST["date_day"]; ?>/<?php echo $_POST["date_month"]; ?>/<?php echo $_POST["date_year"]; ?></td></tr>
    <tr><td>Departure Date:</td><td><?php echo $_POST["dateto_day"]; ?>/<?php echo $_POST["dateto_month"]; ?>/<?php echo $_POST["dateto_year"]; ?></td></tr>
    <tr><td>Rooms required:</td><td><?php echo $_POST["single"]; ?> Single Room(s), <?php echo $_POST["double"]; ?> Double Room(s), <?php echo $_POST["twin"]; ?> Twin Room(s)</td></tr>
    <tr><td>Party Size:</td><td><?php echo $_POST["adults"]; ?> Adult(s), <?php echo $_POST["children"]; ?> Children</td></tr>
    <tr><td>Age(s) of Children:</td><td><?php echo $_POST["childrenage"]; ?></td></tr>
    <tr><td>Message:</td><td><?php echo nl2br($_POST["message"]) ?></td></tr>
    <tr><td colspan="2"><b>Enquiry Details</b></td></tr>
    <tr><td>First Name:</td><td><?php echo $_POST["firstname"]; ?></td></tr>
    <tr><td>Last Name:</td><td><?php echo $_POST["lastname"]; ?></td></tr>
    <tr><td>Email:</td><td><?php echo $_POST["email"]; ?></td></tr>
    <tr><td>Phone:</td><td><?php echo $_POST["phone"]; ?></td></tr>
    <tr><td>Fax:</td><td><?php echo $_POST["fax"]; ?></td></tr>
    <tr><td>Address:</td><td><?php echo $_POST["addressline1"]; ?><br />
            <?php echo $_POST["addresline2"]; ?></td></tr>
    <tr><td>City:</td><td><?php echo $_POST["city"]; ?></td></tr>
    <tr><td>Country:</td><td><?php echo $_POST["country"]; ?></td></tr>
    </table>
    </html>
    
    <?php
    $message = ob_get_clean();
    mail($to,$subject,$message,$headers);
    ?>
    PHP:
     
    danx10, May 7, 2010 IP
    webrickco likes this.
  6. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Indeed the best way to do it!

     
    webrickco, May 7, 2010 IP
  7. lorkan

    lorkan Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Another way, which is good in the beginning, when things don't work is to define the $_POST['xxx'] as variables:
    $name=$_POST['name'];
    $address=$_POST['address'];
    ...
    etc.
    Code (markup):
    then include it in the code as this:
    $msg= '
    <html>
    <table border="1">
    <tr><td colspan="2"><b>' . $name . '</b></td></tr>
    ...
    etc
    
    Code (markup):
    This is just an alternative way for easier keeping track of the variables in the beginning...
     
    lorkan, May 7, 2010 IP
  8. koglaa

    koglaa Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thats what my coder uses O:. Seems to be doing good so far
     
    koglaa, May 7, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    That may-be, but in this particular event I'd suggest output buffering. - refer to my previous post.
     
    danx10, May 7, 2010 IP
  10. kidrobot

    kidrobot Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    nice, i try the form
     
    kidrobot, May 7, 2010 IP
  11. bradleykirwan

    bradleykirwan Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks for your help!
     
    bradleykirwan, May 7, 2010 IP