红酒泡绿茶 2012-10-22 19:46
浏览 27
已采纳

联系表。 如何将该名称与电子邮件一起提供?

It's my first time trying to make a contactform. And I've got a few problems It's works, I get the email, but I don't get the name the name field with me in the email.

HTML:

<form method="post" name="contactform" action="scripts/contact.php">
<h3>Name</h3>
<input type="text" name="name">
<h3>Email Address</h3>
<input type="text" name="email">
<h3>Message</h3>
<textarea name="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form">
</form>

PHP:

<?php
$to = "name@domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $name, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>

$name is my problem. I've I have it in, the email comes from hostmaster@domane.com, If I delete it, everything works fine. But I wan't the name to be sent to me. How? Or should I do it completely different?

Also, if you leave all the fields blank, the "user" doesn't get any error message, and a blank email is sent to me.

Hope you can help me. :)

  • 写回答

3条回答 默认 最新

  • dongzhenyin2001 2012-10-22 19:52
    关注

    Michael Berkowski is correct. What you'll need to do is add the name to your message's body (not in the sense of the input name= attribute, rather the body of the email).

    Something like this:

    <?php
    $to = "name@domane.com";
    $subject = "Contact Us";
    $name = $_POST['name'];
    $email = $_POST['email'] ;
    $message = $_POST['message'] ;
    $headers = "From: $email";
    
    $body = "Name: $name
    ";
    $body .= "Message: $message";
    
    $sent = mail($to, $subject, $body, $headers) ;
    if($sent)
    {print "Your mail was sent successfully"; }
    else
    {print "One of the field are not filled as requirred"; }
    ?>
    



    Revised:

    HTML:

    <form method="post" name="contactform" action="scripts/contact.php">
    <label for="name">Name</label>
    <input type="text" name="name" id="name" />
    <label for="email">Email Address</label>
    <input type="text" name="email" id="email" />
    <label for="message">Message</label>
    <textarea name="message" id="message"></textarea>
    <br/><input class="submit" type="submit" value="Send Form" />
    </form>
    

    PHP:

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'] ;
    $message = $_POST['message'] ;
    
    $body = "Name: $name
    ";
    $body .= "Message: $message";
    
    $to = "name@domane.com";
    $from = "automailer@mydomainname.com (Website Automailer)";
    $subject = "Contact Us";
    $headers =  "From: $from
    " .
            "Reply-To: $email ($name)";
    $sent = mail($to, $subject, $body, $headers) ;
    if($sent) { echo "Your mail was sent successfully"; }
    else { echo "One of the field are not filled as requirred"; }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab