Jump to content

New to PHP and struggling with a form.


martinstan

Recommended Posts

Hi
Sorry for a painfully basic question but I'm having problems with this form. It kind of works but instead of the information collected in the fields being in the body of the email, it's appearing as the mail subject???
Please help!

Thanks
Martin

[code]<?php
$to      = "[email protected]";
$name    = $_POST['name'];
$tel   = $_POST['tel'];
$email    = $_POST['email'];
$msg      = $_POST['msg'];
$d        = date('l dS \of F Y h:i:s A');
$sub      = "Contact from website";
$headers  = "From:" .$name .$email."\n"; 
$mes    .= "Message: ".$msg."\n";
$mes    .= "Name: ".$name."\n";
$mes .= "Tel: ".$tel."\n";
$mes    .= 'Email: '.$email."\n";
$mes    .= 'Date & Time: '.$d;

if (empty($name) || empty($tel) || empty($email) || empty($msg))
{
    echo "  <h3>Sorry all fields are required.</h3>";
}
elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
    print "  <h3>Sorry the email address you entered looks like it's invalid.</h3>";
}
else
{
    mail($to, $mes, $headers);
    print "  <h3><center>Thank you ".$name." for contacting us.<br>We will get back to you as soon as possible</center></h3>";
}
?>[/code]

the form
[code] <form method="post" action="send.php">
    Name:<br><input name="name" type="text" size="40">
    <br><br>
    Tel:<br><input name="tel" type="text" size="40">
    <br><br>
Email:<br><input name="email" type="text" size="40">
    <br><br>
    Message:<br><textarea name="msg" cols="40" rows="6"></textarea>
    <br><br>
    <input type="reset" value="Reset">&nbsp;<input type="submit" value="Send">
  </form>[/code]
Link to comment
https://forums.phpfreaks.com/topic/33901-new-to-php-and-struggling-with-a-form/
Share on other sites

Try this,

[code=php:0]
$mes    = "Message: ".$msg."\n";
$mes    .= "Name: ".$name."\n";
$mes .= "Tel: ".$tel."\n";
$mes    .= 'Email: '.$email."\n";
$mes    .= 'Date & Time: '.$d;
[/code]

Also remember your mail function should be

$mail = mail($to, $subject, $message, $headers);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.