gerendasi gerendasi - 1 year ago 60
PHP Question

Include fields in message of PHP contact form

Being so bad at PHP, I've decided to post here as a last resort.
I want to add a "who" variable to the message body of emails sent via PHP contact form. The form works fine for name, email and message but the "who" input I would like to be a part of the email message that comes through, as a way to communicate who is being referred.

I have tried to add $who=$_REQUEST['who']; as well as $who to the mail line but neither work, the latter doesn't even send an email at all.

if ($action=="") /* display the contact form */
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">

<input name="name" type="text" placeholder="Your Name" value="" size="14"/>
<input name="email" type="text" placeholder="Your Email" value="" size="14"/>
<textarea name="who" placeholder="Who should we contact?" rows="1" cols="14"></textarea>
<textarea name="message" placeholder="Description" rows="2" cols="14"></textarea><br>
<input type="submit" class="button special" value="SUBMIT"/>
if (($name=="")||($email=="")||($message==""))
echo "All fields are required, please fill out <a href=\"\">the form</a> again.";
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Referral for ******* **";
mail("chris@********", $subject, $message, $from);
echo "<script type='text/javascript'>window.location.href ='../thanks.php';</script>";

Answer Source

In PHP . is the concatenation operator which returns the concatenation of its right and left arguments Try this

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'] . "\n\rFrom: " . $_REQUEST['who'];