为什么我的PHP脚本不起作用?

时间:2012-10-17 12:47:32

标签: php forms html5 web

编辑:显然我需要付费才能在其网站上注册另一个域以获得电子邮件功能。很抱歉浪费时间,感谢代码修复。

我对php没有任何经验。我刚刚开始明白这一点。我想让我的表单将输入的信息发送到我的电子邮箱。我设置了一个else标签,但就是这样。如果您发现任何错误,请告诉我们。我真的希望这个工作。亲眼看看:Website代码为:

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!更新了代码。

8 个答案:

答案 0 :(得分:4)

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

而不是方法=&#34;请求&#34;

编辑添加 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等]

答案 1 :(得分: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

答案 2 :(得分:3)

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

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

并使用以下命令访问PHP中的字段:

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

我希望这会对你有所帮助。

答案 3 :(得分: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 settingsphp.ini)或服务器的限制相关(与您的托管公司联系)。

EDIT1 :关于您的html表单,type的{​​{1}}属性应为input fields而不是text或{{1} }。

而不是nameemail

答案 4 :(得分:1)

textarea的namecomment,您在此行中将其用作message

  $message = $_REQUEST['message'];

需要,

 $message = $_REQUEST['comment'];

此外,表单提交的方法需要POST这样,

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

答案 5 :(得分:1)

请按以下方式更改表格

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

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

答案 6 :(得分: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);
}

答案 7 :(得分:1)

- &GT;使用 POST 代替 请求

- &GT; 评论追溯到消息

<强> 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>'; 
    }}
?>