提交联系表格

时间:2019-07-17 04:30:22

标签: php forms submission

我是php的新手-我正在尝试使用提供的模板代码来进行简单的联系表单提交。尽管进行了多次编辑,但仍未获得500内部服务器错误,但仍未成功。

我需要哪一行代码来使服务器提交表单?

因此,我正在尽全力解决这个问题。我对可能导致这种情况的发生有两种想法。

  1. 该php文件位于服务器上的单独文件夹中,是否可能导致此问题?

  2. 我不确定我是否包含在正确的代码中以启动php代码进行提交操作。我已经试过了:

    if(isset($_POST['submit'])) {
    $to = "myemail.com";
    $subject = "Subject Line";

还有这个:

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

这是HTML表单代码:

<form method="POST" action="assets/php/mail.php">
    <div class="row gtr-uniform">
        <div class="col-6 col-12-xsmall">
            <input type="text" name="name" id="name" value="" placeholder="Name" />
        </div>
        <div class="col-6 col-12-xsmall">
            <input type="email" name="email" id="email" value="" placeholder="Email" />
        </div>
        <div class="col-12">
            <select name="category" id="category">
                <option value="">I want a . . .</option>
                <option value="1">Logo</option>
                <option value="1">Website</option>
                <option value="1">Polished Document</option>
                <option value="1"></option>
            </select>
        </div>
        <div class="col-4 col-12-small">
            <input type="radio" id="priority-low" name="priority" checked>
            <label for="priority-low">Low</label>
        </div>
        <div class="col-4 col-12-small">
            <input type="radio" id="priority-normal" name="priority">
            <label for="priority-normal">Normal</label>
        </div>
        <div class="col-4 col-12-small">
            <input type="radio" id="priority-high" name="priority">
            <label for="priority-high">High</label>
        </div>
        <div class="col-12">
            <textarea name="message" id="message" placeholder="Enter your message" rows="6"></textarea>
        </div>
        <p>
            This site is protected by reCAPTCHA and the Google
            <a href="https://policies.google.com/privacy">Privacy Policy</a> and
            <a href="https://policies.google.com/terms">Terms of Service</a> apply.                                         
        </p>
        <div class="col-12">
            <ul class="actions">
                <li><input type="submit" value="Send Message" class="primary" /></li>
                <li><input type="reset" value="Reset" /></li>
            </ul>
        </div>
    </div>
</form>

用于表单提交的php代码:

<?php

    if (isset($_POST['submit'])) {
        $to = "myemail.com";
        $subject = "Subject Line";

        // Sender Data
        $name = str_replace(array("\r", "\n"), array(" ", " "), strip_tags(trim($_POST["name"])));
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $category = $_POST(["category"]);
        $priority = $_POST(["priority"]);
        $message = trim($_POST["message"]);

        if (empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($category)) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo '<p class="alert alert-warning">Please complete the form and try again.</p>';
            exit;
        }

        // Mail Content
        $content = "Name: $name\n";
        $content .= "Email: $email\n\n";
        $content .= "I want a: $category\n\n"
        $content .= "Priority: $priority\n\n"
        $content .= "Message:\n$message\n";


        // email headers.
        $headers = "From: $name <$email>";

        // Send the email.
        $success = mail($mail_to, $content, $headers);
        if ($success) {
            // Set a 200 (okay) response code.
            http_response_code(200);
            echo '<p class="alert alert-success">Thank You! Your message has been sent.</p>';
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send your message.</p>';
        }
    }
    } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo '<p class="alert alert-warning">There was a problem with your submission, please try again.</p>';
    }


?>

在php表单中,如果用户未完全填写表单,则将出现验证错误消息。提交所有必需的信息后,该消息应转到我的电子邮件中,并将用户重定向到首页。

如果需要进一步说明,请告诉我。

2 个答案:

答案 0 :(得分:0)

您的代码有语法错误

这里:

$content .= "I want a: $category\n\n" // => missing ;
$content .= "Priority: $priority\n\n" // => missing ;

在这里:

        echo '<p class="alert alert-warning">Oops! Something went wrong, we couldnt send your message.</p>';
    }
} // => closing bracket not matching anything

启用显示错误

要能够看到致命错误,例如语法错误,请确保在php.ini中启用了display_errors。如果您不知道在哪里可以找到此文件,可以通过调用phpinfo()并在其输出中查找“已加载的配置文件”来找到php.ini的位置。

对于所有非致命错误,您可以直接使用error_reporting()display_errors()在脚本中显示错误:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

使用PHPStorm之类的IDE甚至可以在运行代码之前帮助您设置格式和显示错误。

答案 1 :(得分:0)

mail.php文件中第15和16行出现错误:

您的代码:

$category = $_POST(["category"]);
$priority = $_POST(["priority"]);

更新密码:

$category = $_POST["category"];
$priority = $_POST["priority"];

只需移除方括号“(”,“)”

在form.php文件中我对您有个建议: 使用按钮而不是像这样输入:

<button type="submit" name="submit">Submit