1

我有一个 html 表单,用户将在其中提交他们的联系信息,以便我们与他们取得联系。我希望所有这些都在index.html,但我不反对必须在文件夹中创建另一个文件。

<form class="row" method="post" action="contact.php">
    <div class="col-md-12">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Your Full Name">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Phone Number*">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Email Address*">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Company Name*">
        </div>
        <div class="form-group">
            <textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>
        </div>
    </div>

    <div class="buttn_outer">
        <button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left;">Submit</button>
    </div>
</form>

我已经在服务器机器上安装了 php。查看我发现尝试使用 php 脚本的其他示例,如下所示。

<?php

if($_POST["submit"]) {
    $recipient = "me@me.com";
    $subject = "Some Message subject";
    $sender = $_POST["sender"];
    $senderEmail = $_POST["senderEmail"];
    $message = $_POST["message"];

    $mailBody = "Name: ".$sender."\nEmail: ".$senderEmail."\n\n".$message;

    mail($recipient, $subject, $mailBody, "From: ".$sender." <".$senderemail.">");
    $thankYou = "<p>Thank you! Your message has been sent.</p>";
}
?>

截至目前,它会发送成功消息,但是我从来没有收到任何东西可以发送到我的电子邮件。我对 php 非常陌生,感谢任何帮助/建议。

4

3 回答 3

2

None of your input fields have name attributes.

<input type="text" class="form-control" placeholder="Email Address*">
<input type="text" class="form-control" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" placeholder="Questions / comments"></textarea>

Should be

<input type="text" class="form-control" name="senderEmail" placeholder="Email Address*">
<input type="text" class="form-control" name="sender" placeholder="Your Full Name">
<textarea class="form-control lg_textarea" name="message" placeholder="Questions / comments"></textarea>

Basically, the name attribute of the input field should match what the $_POST key is.

So, <input name="name" /> would be handled as $_POST['name']

Also, it looks like you're mixing up the $recipient and $senderEmail variables. PHP's mail function specifies that the first parameter is the recipient. I understand that me@me.com is clearly not a real email. But, the recipient should be the user's email that he/she has submitted in the form. Make sure the mail function works by testing it without submitting the form and if it does, then try to make it work with the form variables.

于 2014-10-08T03:47:03.520 回答
0

试试这个可以帮助你...

index.html  

 <form class="row" method="post" action="contact.php">
        <div class="col-md-12">
            <div class="form-group">
                <input type="text" class="form-control" name="sender" placeholder="Your Full Name">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="phno" placeholder="Phone Number*">
            </div>
            <div class="form-group">
                <input type="text" class="form-control" name="email" placeholder="Email Address*">
            </div>
            <div class="form-group">
                <input type="text" class="form-control"  name="cmp" placeholder="Company Name*">
            </div>
            <div class="form-group">
                <textarea class="form-control lg_textarea" name="cmt" placeholder="Questions / comments"></textarea>
            </div>
        </div>

        <div class="buttn_outer">
            <button type="submit" class="btn signin_btn col-sm-5 clearfix" style="margin:0 0 0 4%; float: left";>Submit</button>
        </div>
    </form>

    contact.php

    <?php
        $to='me@me.com';
        $header = "From: info@".$_SERVER["SERVER_NAME"]."\n";
        $header .= "Content-Type: text/html; charset=iso-8859-1\n";

        $sender=$_POST['sender'];
        $email=$_POST['email'];
        $cmt=$_POST['cmt'];
        $cmp=$_POST['cmp'];
        $phno=$_POST['phno'];

        $subject='Message Subject';

        $body='<table width="90%" border="0">
        <tr>
        <td><b>Sender:</b></td> <td>'.$sender.'</td>
        </tr>
        <tr>
        <td><b>Email:</b></td> <td>'.$email.'</td>
        </tr>
        <tr>
        <td><b>Phone No:</b></td> <td>'.$phno.'</td>
        </tr>
        <tr>
        <td><b>Copmany Name:</b></td> <td>'.$cmp.'</td>
        </tr>
        <tr>
        <td><b>Comment:</b></td> <td>'.$cmt.'</td>
        </tr>
        <tr></table>';



        mail($to,$subject,$body,$header);
       header('location:index.html');



    ?>
于 2014-10-08T04:03:17.527 回答
0

其他的,我会扔的

($_POST["submit"]) 应该改为

!empty($_POST["submit"]) - 或者 - isset($_POST["submit"])

永远不要假设变量已设置 - 在您检查它是否已设置(或您自己初始化)之前,不要对其运行任何测试或使用它的值。

根据您的 PHP 配置,该行可能会失败。因此,如果您将您的contact.php 页面移动到另一台服务器,那么它可能会因错误而崩溃。

正如 Lance 所提到的,您想要传回服务器的每个表单字段都需要一个名称属性。$_POST当它被传递到服务器时,这就是将它链接到数组的原因。

此外,您可以使用用户电子邮件作为发件人地址。但是,当接收服务器发现它不是来自该用户的域电子邮件服务器时,您的电子邮件可能会被标记为垃圾邮件。如果您试图“欺骗”它(也就是不使用服务器的默认值),您将不得不使用服务器的电子邮件设置,以便将其验证为合法的发件人地址。

如果您想覆盖默认的发件人地址(或回复地址),您需要在标题字段中执行此操作(您可以谷歌这个以查看您的选项)。

如果你想在你的 HTML 中包含 HTML 标签或元素,你还需要在 mail 函数的 headers 参数中指出这一点。对于简单的警报电子邮件,例如您正在做的事情 - 我只使用纯文本并使用回车符(就像您使用\n's 一样)并且不使用<p></p>.

于 2014-10-08T04:01:56.450 回答