doujiu3097 2014-02-18 02:30
浏览 38
已采纳

无法从此php表单获取电子邮件

I'm making a email form on my website. For some reason, I cannot get email from this form.

html

        <form method="POST" action="processor.php" id="contactform" onsubmit="return(validateForm());">
        <div>
            <label for="fname">First Name:<br></label>
            <input type="text" name="fname" id="fname" value="" class="required"/>
        </div>
        <div>
            <label for="lname">Last Name:<br></label>
            <input type="text" name="lname" id="lname" value="" class="required"/>
        </div>
        <div>
            <label for="organization">Organization:<br></label>
            <input type="text" name="organization" id="organization" value="" class="required"/>
        </div>
         <div>
            <label for="email">Email:<br></label>
            <input type="text" name="email" id="email" value="" class="required email" />
        </div>

        <div>
            <label for="phone">Phone:<br></label>
            <input type="text" name="phone" id="phone" value="" class="required" />
        </div>          

        <div>
            <label for="message" style="line-height: 2;">Message:<br></label>
            <textarea rows="20" cols="20" name="message" id="message"></textarea>
        </div>

        <input id="submit" name="submit" type="submit" class="submit-button myButton" value="Send" />
        <div class="clearfix"></div>
    </form>

PHP

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 7 || !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
        die("Bad referer");
    $msg = "Website Message:


";
    foreach ($_POST as $key => $val) {
        if (is_array($val)) {
            $msg .= "Item: $key

";
            foreach ($val as $v) {
                $v = stripslashes($v);
                $msg .= " $v

";
            }
        } else {
            $val = stripslashes($val);
            $msg .= "$key: $val

";
        }
    }
    $recipient = "test@example.com";
    $subject   = "Website Contact Form Filled Out";
    error_reporting(0);
    if (mail($recipient, $subject, $msg)) {
        header("Location: http://www.example.com/thankyou.html");
    } else
        echo "An error occurred and the message could not be sent.";
} else
    echo "Bad request method";
?>

The thing is the PHP code works because the it worked with the other HTML form. I am wondering the way of HTML coding is the reason or not. If anybody know PHP and form well, please help me. Thank you!

  • 写回答

1条回答 默认 最新

  • duanmuyao0463 2014-02-18 03:19
    关注

    Try this:

    print_r($_POST);
    exit;
    

    Before your code

    // put it here
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
            if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 7 || !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
                die("Bad referer");
        ....
    

    If you will see variable contained email - then something wrong with your PHP script And also - Enable showing errors (i.e. error reporting)

    I saw, you said it's works perfectly with another form. Do i can look at code of this form, please?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码