简体   繁体   English

PHP的形式不断给我500错误

[英]php form keeps giving me 500 error

I'm having trouble getting my contact form to work. 我无法使用我的联系表。 I'm new to php and this is the first mailer I've created, so I could definitely use some help. 我是php的新手,这是我创建的第一个邮件程序,因此我绝对可以使用一些帮助。

Here's my HTML form 这是我的HTML表格

        <form id="ajax-contact" method="post" action="mailer.php">
            <div class="column">
                <label for="name">name</label>
                <input type="text" id="name" name="name" required>
                <label for="email">email address</label>
                <input type="email" id="email" name="email" required>
            </div>

            <div class="column">
                <label for="message">message</label>
                <textarea id="message" required></textarea>
            </div>

            <button type="submit">Send</button>
        </form>

Here's my mailer.php 这是我的mailer.php

<?php
    // My modifications to mailer script from:
    // http://blog.teamtreehouse.com/create-ajax-contact-form
    // Added input sanitizing to prevent injection


    // Only process POST reqeusts.
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Get the form fields and remove whitespace.
        $name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
        $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
        $message = trim($_POST["message"]);


        // Check that data was sent to the mailer.
        if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // Set a 400 (bad request) response code and exit.
            http_response_code(400);
            echo "Oops! There was a problem with your submission. Please complete the form and try again.";
            exit;
        }


        $recipient = "thomas.lacroix.e@gmail.com";


        $subject = "New message from $name";


        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Message:\n$message\n";


        $email_headers = "From: $name <$email>";


        // Send the email.
        if (mail($recipient, $subject, $email_content, $email_headers)) {
            // Set a 200 (okay) response code.
            http_response_code(200);
            echo "Thank You!";
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Something went wrong.";
        }


    } else {
        // Not a POST request, set a 403 (forbidden) response code.
        http_response_code(403);
        echo "There was a problem with your submission, please try again.";
    }
?>

I've searched for people with similar problems and I believe I need to add something with my email host settings. 我搜索了存在类似问题的人员,并且我相信我需要在电子邮件主机设置中添加一些内容。 Let's be honest, though, I don't know what I'm doing. 老实说,不过,我不知道自己在做什么。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

It looks like on your form you forgot to include the name for the textarea which is causing an undefined index in the code when trying to get 看起来您在表单上似乎忘记了文本区域的名称,这在尝试获取时会在代码中导致未定义的索引

$_POST['message']

<textarea name="message" id="message" required></textarea>

Doing the above resolved the issue in testing for me. 这样做为我解决了测试中的问题。

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

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