简体   繁体   English

努力使联系表单正常工作(HTML5 / PHP)

[英]Struggling to make contact form work (HTML5/PHP)

I'm having some difficulty getting my contact form working on my website. 我在联系表格在我的网站上工作时遇到了一些困难。 Whenever I try submit, it opens index.php, a blank page, rather than actually executing the script and firing off an email. 每当我尝试提交时,它都会打开index.php(空白页),而不是实际执行脚本并发出电子邮件。

Here's the form. 这是表格。

    <form method="post" action="index.php">
        <div class="field">
            <label for="name">Name</label>
            <input type="text" name="name" id="name" />
        </div>
        <div class="field">
            <label for="email">Email</label>
            <input type="email" name="email" id="email" />
        </div>
        <div class="field">
            <label for="message">Message</label>
            <textarea name="message" id="message" rows="4"></textarea>
        </div>
        <ul class="actions">
            <li><input type="submit" type="submit" value="Send Message" /></li>
        </ul>
    </form>

Here's the index.php file I'm using. 这是我正在使用的index.php文件。

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Test'; 
$to = 'brett@edge.yt'; 
$subject = 'Hello';

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

PHP is seemingly properly installed on my server, I can use a PHPInfo() file no problem. PHP在我的服务器上看似正确安装,我可以使用PHPInfo()文件没问题。

The email will never try to send because $_POST['submit'] is not set. 该电子邮件将永远不会尝试发送,因为未设置$_POST['submit'] You don't have a control on your form named submit . 您的表单上没有控件submit If you intended that to be the submit button, you need to give it a name attribute. 如果您希望将其用作“提交”按钮,则需要为其指定name属性。

<input type="submit" name="submit" value="Send Message" />

Then, in your PHP script, check that submit isset rather than just evaluating it as a boolean. 然后,在您的PHP脚本中,检查isset而不只是将其评估为布尔值。

if (isset($_POST['submit'])) { ...

That way, if the form hasn't been submitted you won't get an undefined index notice. 这样,如果尚未提交表单,则不会得到未定义的索引通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM