PHP联系表单没有内容

时间:2015-07-02 15:48:55

标签: php html forms

我尝试使用的联系表单只发送主题字段但没有内容。

我使用以下表格:

<form id="contact-form-face" class="clearfix"      action="http://www.demo.com/php/contactengine.php">
                            <input type="text" name="email" value="Email"     onFocus="if (this.value == 'Email') this.value = '';" onBlur="if (this.value == '') this.value = 'Email';" />
                            <textarea name="message" onFocus="if (this.value     == 'Message') this.value = '';" onBlur="if (this.value == '') this.value = 'Message';">Message</textarea>
                            <input class="contact_btn" name="submit"     type="submit" value="Send Message" />
                        </form>

PHP帖子:

<?php

$EmailFrom = "myemail";
$EmailTo = "myemail";
$Subject = "";
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// validation
$validationOK=true;
if (!$validationOK) { 
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

// prepare email body text
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

任何帮助都会有很大的学徒性

2 个答案:

答案 0 :(得分:4)

您未在表单上定义方法,这会产生&#34;消息&#34;和&#34;电子邮件&#34;值作为GET参数发送,这意味着它们成为URL的一部分的查询参数。为了让表单将其输入发送到$ _POST,您必须像这样设置表单操作:

<form id="contact-form-face" class="clearfix" action="http://www.demo.com/php/contactengine.php" method="post">

此外,您在接收端有一个拼写错误,在那里您查找$ _POST [&#34;消息&#34;]但在表单中您将名称指定为&#34; message&#34;。这些必须匹配。

//编辑 - 要实现警报弹出窗口而不是重定向,请更改脚本末尾的if条件,如下所示:

if ($success) {
    ?>
    <script>
        alert("Success!");
    </script>
    <?php
}
else{
    ?>
    <script>
        alert("Failure!");
    </script>
    <?php
}

答案 1 :(得分:1)

您正在使用

$_POST['Message']

应该是:

$_POST['message']