简体   繁体   中英

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.

<?php
    $action=$_REQUEST['action'];
    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"/>
</form>
<?php
    }
    else
    {
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $message=$_REQUEST['message'];
        if (($name=="")||($email=="")||($message==""))
        {
            echo "All fields are required, please fill out <a href=\"\">the form</a> again.";
    }
    else{
        $from="From: $name<$email>\r\nReturn-path: $email";
        $subject="Referral for ******* **";
        mail("chris@********.com.au", $subject, $message, $from);
    }
    {
        echo "<script type='text/javascript'>window.location.href ='../thanks.php';</script>";
    }
}
?>

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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM