【问题标题】:HTML5 Contact form is submitting without any data in fieldsHTML5 联系表单正在提交,但字段中没有任何数据
【发布时间】:2018-04-15 09:36:45
【问题描述】:

我第一次在这里发帖,如果我有任何错误,请大家见谅。我最近使用 HTML5 UP 模板构建了一个站点 - www.vancareleeds.co.uk。我使用了模板附带的联系表格,并使用根文件夹中的一个简单 mail.php 文件来处理要发送到我的收件箱的电子邮件。

我也输入了 Google ReCaptcha,但我一直在努力强制表单验证(例如,可以在不勾选 reCaptcha 的情况下发送表单,也可以在表单字段中没有信息的情况下发送表单。

我在这里提供了我的 .php 代码以及网页本身。

如果我违反了 psot 的协议或最佳实践,我深表歉意。

mail.php

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mobile = $_POST['mobile'];
$vehicle = $_POST['category-vehicle'];
$service = $_POST['category-service'];
$formcontent="From: $name \n Message: $message \n Mobile: $mobile \n Vehicle: $vehicle \n Service Required: $service";
$recipient = "info@vancareleeds.co.uk";
$subject = "Contact Form from VanCare Website";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

表单html

<form method="post" action="mail.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>
    <div class="g-recaptcha" data-theme="dark" class="g-recaptcha" data-sitekey="6Lf1OVMUAAAAAJv1fNtt-CJEFPK-Q0Ugc1CVCRVh"></div><br/>
    <ul class="actions">
    <li><input type="submit" value="Send Message" /></li>
    </ul>                       
</form>

【问题讨论】:

  • 您的 html 中似乎缺少类别(类别车辆和类别服务)和移动设备的输入。

标签: php html forms dreamweaver contact


【解决方案1】:

欢迎+加泰罗尼亚足球!如果您是 PHP 新手,我建议您使用 var_dump()print_r()var_export() $_POST 全局数组的结果来查看发送到您的 PHP 脚本的内容。如果您有信心,可以注释掉所有其他内容并取消注释。

$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['name'], $_POST['email'], $_POST['message']) === true) {
    $name = filter_var(trim($_POST['name']), FILTER_SANITIZE_STRING);

    if (empty($name) === true) {
        $errors['name'][] = 'Name is empty';
    } elseif (ctype_alpha($name) === false) {
        $errors['name'][] = 'Name contains invalid characters';
    }

    $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);

    if (empty($email) === true) {
        $errors['email'][] = 'E-mail is empty';
    } elseif ($email === false) {
        $errors['email'][] = 'Invalid e-mail address';
    }

    $message = strip_tags(trim($_POST['message']));

    if (empty($message) === true) {
        $errors['message'][] = 'Message is empty';
    }

    if (count($errors) === 0) {
        echo 'Thank you!';
        // you can use here the sanitized user input to send the e-mail
    }
}

if (count($errors) > 0) {
    foreach ($errors as $field => $messages) {
        echo implode(', ', $messages), '<br>';
    }
}

这将测试用户输入并打印出错误消息。

【讨论】:

  • 哇,非常感谢,抱歉,如果这是一个愚蠢的问题,但我应该把这段代码放在哪里,在现有代码下的 mail.php 中?只是想知道最终的 html 和 php 代码在各自文件中的外观...
  • 将此源代码放在mail.php 中&lt;?php ... ?&gt; 中的任何其他内容之前,不要忘记在if (count($errors) === 0) { .... } 中插入您的邮件发送功能。我必须告诉您,当您检查 Google 验证码是否有效时,此源代码不包含该部分。没有愚蠢的问题,如果您需要帮助,请随时询问。
猜你喜欢
  • 2018-06-30
  • 2017-03-26
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 2020-01-05
  • 1970-01-01
相关资源
最近更新 更多