1

我目前正在开发一个使用 PHP 将消息发送到电子邮件的 HTML 表单。我正在 MAMP 中进行测试,单击“发送消息”后无法得到响应。任何建议将不胜感激。

HTML

<form action="mail.php" method="POST">
    <div class="row half">
        <div class="6u">
            <input type="text" class="text" name="name" placeholder="Name" />
        </div>
        <div class="6u">
            <input type="text" class="text" name="email" placeholder="Email" />
        </div>
    </div>
    <div class="row half">
        <div class="12u">
            <textarea name="message" placeholder="Message" rows="6" cols="25"></textarea>
            <br />
        </div>
    </div>
    <div class="row">
        <div class="12u">
            <ul class="actions">
                <li class="button form" input type="submit" value="Submit">Send Message</li>
            </ul>
        </div>
    </div>
</form>

PHP

<?php 
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $to = 'my@email.com';
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    $send_message=mail($to, $subject, $formcontent, $mailheader);

    if($send_message){
        echo "thank you"
    } else {
        echo "error";
    }
?>

谢谢

4

3 回答 3

5

在你的代码中发现了一些错误

纠正这个:

<li class="button form" input type="submit" value="Submit">Send Message</li>

<li class="button form"> <input type="submit" value="Submit">Send Message</li>

纠正这个:

 echo "thank you" 

echo "thank you";

于 2013-10-31T06:47:00.937 回答
0

您是否在本地服务器中检查此内容?php mail() 函数可能在本地服务器上不起作用。在一些在线服务器中检查它(在更正@Ashish 提到的错误之后)。

于 2013-10-31T07:01:49.843 回答
0

HTML

<form action="mail.php" method="POST">
<div class="row half">
    <div class="6u">
        <input type="text" class="text" name="name" placeholder="Name" />
    </div>
    <div class="6u">
        <input type="text" class="text" name="email" placeholder="Email" />
    </div>
</div>
<div class="row half">
    <div class="12u">
        <textarea name="message" placeholder="Message" rows="6" cols="25"></textarea>
        <br />
    </div>
</div>
<div class="row">
    <div class="12u">
        <ul class="actions">
            <li class="button form"> <input type="submit" value="Submit" name="sendmessage"/>Send Message</li> // Use name attribute
        </ul>
    </div>
</div>

*使用名称属性*

PHP

      <?php 
    if($_POST['sendmessage']!="")
    {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $formcontent="From: $name \n Message: $message";
        $to = 'my@email.com';
        $subject = "Contact Form";
        $mailheader = "From: $email \r\n";
        $send_message=mail($to, $subject, $formcontent, $mailheader);

        if($send_message){
            echo "thank you";
        } else {
            echo "error"; } 
 }
?>

试试这个代码。

于 2013-10-31T07:03:22.413 回答