Jump to content

Recommended Posts

Hey all, I just can't see what's wrong with my form/php. I fill in the form, and it gets sent to the correct address and has the correct return address and the message but the Name and Phone fields are not included in the email.

 

Here's the HTML

 

<form method="post" action="contact.php" name="contact" id="contact">
  <table width="496" border="0">
        <tr>
          <td width="83"><strong>Name:</strong></td>
          <td width="403"><input name="name" type="text" size="40" /></td>
        </tr>
        <tr>
          <td><strong>Telephone:</strong></td>
          <td><input name="phonenumber" type="text" size="40" /></td>
        </tr>
        <tr>
          <td><strong>Email:</strong></td>
          <td><input name="email" type="text" size="40" /></td>
        </tr>
        <tr>
          <td valign="top"><strong>Message:</strong></td>
          <td><textarea name="message" cols="30" rows="5"></textarea></td>
        </tr>
        <tr>
          <td> </td>
          <td><input type="submit" name="Submit" value="Submit" />
            <input type="reset" name="Reset" value="Reset" /></td>
        </tr>
      </table>
      </form>

 

And the PHP

 

<?php
$to = "myemail@mydomain.com";
$subject = "Contact Enquiry";
$name = $_REQUEST['name'] ;
$phone = $_REQUEST['phonenumber'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{$emailResult= "<div>Your email was sent sucessfully</div>"; }
else
{$emailResult= "<div>We encountered an error sending your email</div>";}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>email confirmation</title>
</head>
<body>
<?php echo $emailResult; ?>
</body>
</html>

 

I thought it might be this line $sent = mail($to, $subject, $message, $headers) ;

 

so changed it to: $sent = mail($to, $subject, $name, $phone, $message, $headers) ;

 

but that gave me errors.

 

Can anyone see what I can't?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/151068-solved-php-form-email-missing-info/
Share on other sites

Maybe cuz no where in there do they get added to the message...

You set the variables, but as is, how could you expect them to get included in the message?

I'm going to take a wild guess your new to php.

 

 

The reason mail() gave you an error is because it is only expection 4 strings (and optional additional_parameters). It goes

mail("who it's to", "the subject", "the body of the message which will be in plain text", "and the headers which could make the body HTML rather than plain text")

 

Do you know how to concatinate (combine) strings?

You cant just add extra parameters into a function. You aren't including the fields in the message. Build up the message first:

 

$emailBody  = "Name: ".$_REQUEST['name']."\n";
$emailBody .= "Phone: ".$_REQUEST['phonenumber']."\n";
$emailBody .= "Email: ".$_REQUEST['email']."\n\n";
$emailBody .= $_REQUEST['message']."\n";

mail("myemail@mydomain.com", "Contact Enquiry", $emailBody, $headers);

Thanks guys,

 

Yep new to php and struggle but persevere...

So nope I don't really understand how to concatinate :-/

 

Neil if I copy your code so

 

<?php

$name = $_REQUEST['name'] ;

$phone = $_REQUEST['phonenumber'] ;

$email = $_REQUEST['email'] ;

$message = $_REQUEST['message'] ;

$headers = "From: $email";

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

if($sent)

{$emailResult= "<div>Your email was sent sucessfully</div>"; }

else

{$emailResult= "<div>We encountered an error sending your email</div>";}

?>

 

becomes

 

<?php

$to = "myemail@mydomain.com";

$subject = "Contact Enquiry";

$emailBody  = "Name: ".$_REQUEST['name']."\n";

$emailBody .= "Phone: ".$_REQUEST['phonenumber']."\n";

$emailBody .= "Email: ".$_REQUEST['email']."\n\n";

$emailBody .= $_REQUEST['message']."\n";

$headers = "From: $email";

$sent = mail("myemail@mydomain.com", "Contact Enquiry", $emailBody, $headers);

if($sent)

{$emailResult= "<div>Your email was sent sucessfully</div>"; }

else

{$emailResult= "<div>We encountered an error sending your email</div>";}

?>

 

everything will work?

Tested it out and it does send all the info now so thankyou Niel. One thing though, the way I had it before, the email addresses entered in the form became the return email address but now it's only in the email so is there a way to have the email address entered in the form to become the retunr email as before?

 

Thanks again for your time and help

possibly will work... looks right to me with a quick glance.

the "." is being used as the concatininator

$var1 = "foo";
$var2 = "bar";
echo $foo.$bar; //outputs "foobar"
echo $foo." ".$bar; //outputs "foo bar"
$string  = $foo;
$string .= " fighter"." ";//the dot before the equal sign means you are adding to the string
$string .= $bar; //again, adding further to the string.
echo $string; //outputs "foo fighter bar"

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.