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      = "info@myemailaddress.co.uk";
$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
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);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.