0

编辑:显然我必须付费才能在他们的网站上注册另一个域才能拥有电子邮件功能。很抱歉浪费了时间,并感谢代码修复。

我对php没有任何经验。我才刚刚开始弄清楚。我正在尝试让我的表格将输入的信息发送到我的电子邮件。我设置了一个 else 标签,但这就是发生的一切。如果您发现任何错误,请告诉我。我真的希望这个工作。自己看:网站代码是:

HTML:

<form action="post_comment.php" method="post" id="commentform">

<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>

<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"   required="required"><br/><br/>

<label for="comment" id="comment" class="required">Your Message</label><br/>

<textarea name="comment" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
<input id="send" name="send" type="hidden" value="1" />

</form>

PHP:

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['comment'];
    $from = 'From: '. $email; 
    $to = 'powersjesse@yahoo.com'; 
    $subject = 'WEBSITE';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['send'] == "1") { 
        if (mail($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }}
?>

Something went wrong, go back and try again!当我尝试提交表单时, 我得到了。更新了代码。

4

8 回答 8

4
<form action="post_comment.php" method="post" id="commentform">

而不是方法=“请求”

编辑添加 http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1

编辑(响应编辑的问题代码...)添加http://php.net/manual/en/function.mail.php

您的 mail() 参数顺序错误;应该是 $to, $subject, $message [, $additional_headers, etc.]

于 2012-10-17T12:51:19.507 回答
3
<form action="post_comment.php" method="get" id="commentform">

<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>

<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"   required="required"><br/><br/>

<label for="comment" id="comment" class="required">Your Message</label><br/>

<textarea name="comment" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />

</form>

方法必须是 get/post

于 2012-10-17T12:52:21.600 回答
3

您的“提交”字段是提交表单的 HTML 按钮,不会添加到表单中。添加另一个不可见的输入字段:

<input id="send" name="send" type="hidden" value="1" />

并通过以下方式访问 PHP 中的字段:

if ($_REQUEST['send'] == "1") {

我希望这可以帮助你。

于 2012-10-17T12:53:20.540 回答
3

根据您的代码:

   if ($_REQUEST['submit']) {               
        if (mail($to, $subject, $body, $from))
        { 
           echo '<p>Your message has been sent!</p>';
        }
        else
        { 
           echo '<p>Something went wrong, go back and try again!</p>'; 
        }
   }

获取以下输出:

出了点问题,回去再试一次!

意味着mail()函数出了问题,它返回false,否则我们会看到该Your message has been sent!消息。

mail()函数的问题是它没有显示任何错误或警告,它只是返回false

如何定位问题?

尝试编写如下内容:

    if(mail("your.working.email@gmail.com" , "A subject for example" , "the content of this email","From: no-reply@yourdomain.com"))
     echo "We are good";
    else
     echo "Something not workin";

如果它有效,请在声明它们后立即检查任何已发布变量的值。

   echo $name;
   echo $email;
   echo $message;

确保这些变量不为空,并且该$email变量包含合法且经过验证的电子邮件地址。

如果基本mail用法不起作用 - 这与您的php settings( php.ini) 或您的服务器的限制有关(联系您的托管公司)。

EDIT1:关于您的html表单,type属性input fields应该是text而不是nameemail

而不是<input type="email"...<input type="text"...

于 2012-10-17T13:10:43.150 回答
1

nametextarea 是comment并且您message在这一行中使用它,

  $message = $_REQUEST['message'];

需要是,

 $message = $_REQUEST['comment'];

还有,表单提交的方法,需要是POST这样的,

<form action="post_comment.php" method="post" id="commentform">
于 2012-10-17T12:51:27.937 回答
1

请更改表格如下

<form action="post_comment.php" method="post" id="commentform">

在表单中,您只能使用 get/post

于 2012-10-17T12:53:45.063 回答
1

试试这个

<form action="post_comment.php" method="post" id="commentform">

<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>

<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"    required="required"><br/><br/>

<label for="comment" id="comment" class="required">Your Message</label><br/>

<textarea name="comment" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />

并在您的帖子页面中..

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: '. $email; 
$to = 'powersjesse@yahoo.com'; 
$subject = 'WEBSITE';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";


$headers = "From: $from \r\n";
//$headers .= "Reply-To: $visitor_email \r\n";

mail($to, $subject, $body,$headers);
}
于 2012-10-17T12:58:37.530 回答
1

-> 使用POST而不是REQUEST

->评论更改为消息

HTML

<form action="post_comment.php" method="post" id="commentform">
<label for="comment_author" class="required">Your Name</label>
<input name="name" id="name" tabindex="1" required="required"><br/><br/>
<label for="email" class="required">Your Email</label>
<input type="email" id="email" name="email" id="email" value="" tabindex="2"  required="required"><br/><br/>
<label for="comment" id="comment" class="required">Your Message</label><br/>
<textarea name="message" rows="10" tabindex="4"  required="required"></textarea><br/>
<input id="submit" name="submit" type="submit" value="Submit Comment" />
</form>

PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: '. $email; 
    $to = 'powersjesse@yahoo.com'; 
    $subject = 'WEBSITE';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {               
        if (mail($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }}
?>
于 2012-10-17T13:06:41.500 回答