【问题标题】:PHP Single page contact form not workingPHP单页联系表不起作用
【发布时间】:2013-12-30 11:22:19
【问题描述】:

我一直在进一步处理网页上的联系表单,但无法使其正常运行。 (通过包含并在 apache2.2 本地服务器上运行)。

<?php
if($_SERVER['Request_Method'] != 'POST') {
    $self = $_SERVER['PHP_SELF'];
?>
<form method="POST" action="<?php echo $self; ?>">
    <table>
        <tr>
            <td style="width:100px;">
                Name:
            </td>
            <td>
                <input style="width:150px; height:25px;" placeholder="First Name" type="text" id="custname" name="custname">
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>    
                <input style="width:150px; height:25px;" placeholder="Last Name" type="text" id="cust2name" name="cust2name">
            </td>
        </tr>
        <tr>
            <td>
                Email:
            </td>
            <td>                
                <input style="width:250px; height:25px;" placeholder="Example@domain.com" type="text" id="custemail" name="custemail">
            </td>
        </tr>
        <tr>
            <td>
                Subject:
            </td>
            <td>
                <input style="width:250px; height:25px;" placeholder="RE:Appointment & Contact" type="text" id="textsubject" name="textsubject">
            </td>
        </tr>
        <tr>
            <td>                
                Message:
            </td>
            <td>
                <textarea id="custtext" name="custtext" placeholder="Please enter your message here..." rows="6" style="resize:none; font-family:arial; width:500px; height:75px;"cols="25"></textarea>
            </td>
        </tr>
        <tr>
            <td>

            </td>
            <td>
                <input type="submit" value="Send"><input type="reset" value="Clear">
            </td>
        </tr>
    </table>
</form>
<?php
} else { 
    $name = $_POST['custname'];
    $email = $_POST['custemail'];
    $text = $_POST['custtext'];
    $subject = $_POST['textsubject'];
    $emailto = "reconnectsteam@hotmail.co.uk";

    $header = "From: $name <$email>\r\nReply-To: $email\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type:text/html;charset=iso-8859-1\r\n";

    $message = "From: $name, Email: $email<br /><hr />$text";

    $mail($emailto, $subject, $message, $header);

    echo"Your email has been sent, it will be proccessed within 48hours.";
}

?>

只是想知道这段代码在通过 apache 2.2 启动时是否可以工作(电子邮件不会发送,但其余代码是否仍然可以正常工作并运行?目前看来不是。有什么错误吗?(不包括数据库)。

谢谢,

录音

【问题讨论】:

  • 你有什么错误吗?
  • 注意电子邮件标头注入。
  • 无法从您的本地计算机发送邮件。将它上传到服务器上,它就会工作。
  • 在PHP中,数组键是区分大小写的,所以至少$_SERVER['Request_Method']应该是$_SERVER['REQUEST_METHOD']
  • 感谢心碎和 jedwards 的信息,很高兴知道。今天晚些时候将尝试上传。没有错误只是一个可疑的页面刷新,我将在以后添加安全协议。这只是为了测试目的。

标签: php html forms submit contact


【解决方案1】:

您的代码有两个问题。

这一行:

if($_SERVER['Request_Method'] != 'POST') {

Request_Method 必须大写REQUEST_METHOD,因为它是superglobal

还有$前面的美元mail()

$mail($emailto, $subject, $message, $header);

应该是:

mail($emailto, $subject, $message, $header);

重新格式化和工作的 PHP/HTML 表单

<?php
if($_SERVER['REQUEST_METHOD'] != 'POST') {
    $self = $_SERVER['PHP_SELF'];
?>
<form method="POST" action="<?php echo $self; ?>">
    <table>
        <tr>
            <td style="width:100px;">
                Name:
            </td>
            <td>
                <input style="width:150px; height:25px;" placeholder="First Name" type="text" id="custname" name="custname">
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>    
                <input style="width:150px; height:25px;" placeholder="Last Name" type="text" id="cust2name" name="cust2name">
            </td>
        </tr>
        <tr>
            <td>
                Email:
            </td>
            <td>                
                <input style="width:250px; height:25px;" placeholder="Example@domain.com" type="text" id="custemail" name="custemail">
            </td>
        </tr>
        <tr>
            <td>
                Subject:
            </td>
            <td>
                <input style="width:250px; height:25px;" placeholder="RE:Appointment & Contact" type="text" id="textsubject" name="textsubject">
            </td>
        </tr>
        <tr>
            <td>                
                Message:
            </td>
            <td>
                <textarea id="custtext" name="custtext" placeholder="Please enter your message here..." rows="6" style="resize:none; font-family:arial; width:500px; height:75px;"cols="25"></textarea>
            </td>
        </tr>
        <tr>
            <td>

            </td>
            <td>
                <input type="submit" value="Send"><input type="reset" value="Clear">
            </td>
        </tr>
    </table>
</form>
<?php
} else { 
    $name = $_POST['custname'];
    $email = $_POST['custemail'];
    $text = $_POST['custtext'];
    $subject = $_POST['textsubject'];
    $emailto = "email@domain.com";

    $header = "From: $name <$email>\r\nReply-To: $email\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type:text/html;charset=iso-8859-1\r\n";

    $message = "From: $name, Email: $email<br /><hr />$text";

    mail($emailto, $subject, $message, $header);

    echo "Your email has been sent, it will be proccessed within 48hours.";
}

?>

【讨论】:

    【解决方案2】:

    先尝试替换

    $mail($emailto,...
    

    mail($emailto,...
    

    (去掉“$”)

    【讨论】:

    • 感谢您的更正。但是表单仍然无法正常工作
    • 那么就照 jedwards 说的去做吧。将 $_SERVER['Request_Method'] 替换为 $_SERVER['REQUEST_METHOD']。
    【解决方案3】:

    不完全回答您的问题,但一些全球关注/安全点:)。

    尝试将操作留空(默认情况下,表单会重定向到同一页面)。然后检查是否填写了所有需要的输入,而不是检查$_SERVER['REQUEST_METHOD'] != 'POST'。在这种情况下,您可以使用输入的值填写表格。

    例如,某人只输入姓名。然后邮件不会发送并且用户给出的值消失(有人必须再次输入名称)。 我会提出这样的建议(只有一个字段的简单示例 - 名称)

    $name = isset($_POST['custname']) ? $_POST['custname'] : '';
    $name = htmlspecialchars($name); // !!!!!!!!!!!!!
    if ($name != '' && your_valid_function($name)) { // && custmail test && other test
        SEND MAIL;
        header("Location: this_page"); // prevent refreshing this page and sending multiple mails
    }
    else {
        //Render form here, using $name with filled values, for example:
        echo "<form><input type=\"text\" id=\"custname\" name=\"custname\" value=\"$name\"></form>"; // notice $name here
    }
    

    附言。 在使用 POST 值时始终至少使用 htmlspecialchars :)。小心使用机器人 - 没有验证码或其他一些安全功能的表单可能会给您的邮箱发送垃圾邮件。 希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-27
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      相关资源
      最近更新 更多