PHP的联系方式输出问题

时间:2018-11-22 06:15:42

标签: php contact-form

因此,我正在尝试为网站制作“与我联系”表格,这是我第一次制作这些表格之一。我的问题是,出于某种奇怪的原因,我的按钮没有发送任何东西,并且该按钮也不想工作。

这是输入表单

<div>
        <form id="contact-us" method="post" action="contact-form-handler.php"> 
            <input name="name" type="text"class="form-control" placeholder="Your name" required>
            <br>
            <input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes"required>
            <br>
            <input name="email" type="email" class="form-control" placeholder="Your email" required>
            <br>
            <textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
            <br>
            <input type="submit" class="form-control-submit" value="SEND">
        </form>
    </div>

这是我的管理员。

    <?php
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

    $email_from = 'website.com';
    $email_subject = 'New message from customer';
    $email_body = "name:  .$name. \n". 
                    "email: .$visitor_mail.\n".
                    "phone: .$phone. \n".
                    "message: .$message. \n";
    $to= "email@gmail.com";
    $headers= $email_from.;
    $headers .= $visitor_email.;

    mail($to,$email_subject,$email_body,$headers);

    header("Location: index.html");
?>

我不确定为什么这行不通,但是如果有人可以帮助我,那将是惊人的。

1 个答案:

答案 0 :(得分:0)

<div>
    <form id="contact-us" method="post" action="contact-form-handler.php">
        <input name="name" type="text" class="form-control" placeholder="Your name" required>
        <br>
        <input name="phone" type="number" class="form-control" placeholder="Phonenumber no symbols or dashes" required>
        <br>
        <input name="email" type="email" class="form-control" placeholder="Your email" required>
        <br>
        <textarea name="message" class="form-control" placeholder="write us a message" rows="4" required></textarea>
        <br>
        <input name="submit" type="submit" class="form-control-submit" value="SEND">
    </form>
</div>

处理程序

if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

    $email_from = 'website.com';
    $email_subject = 'New message from customer';
    $email_body = "name:  {$name} \n" .
        "email: {$visitor_email}\n" .
        "phone: {$phone} \n" .
        "message: {$message} \n";
    $to = "email@gmail.com";

    $headers = $email_from;
    $headers .= $visitor_email;

    $res = mail($to, $email_subject, $email_body, $headers);
    header("Location: index.html");
}

我在您的代码中发现了一些错误:

$headers= $email_from.; // syntax error - need to remove dot before ;
$headers .= $visitor_email.; // syntax error - need to remove dot before; same here

在您的电子邮件正文中,您使用了$visitor_mail变量,但您是$visitor_email 另外,我已将名称标签添加到您的表单中,并添加了其他验证条件。

请尝试使用此解决方案,希望对您有所帮助。这种解决方案对我而言效果很好