dqs66973 2015-01-19 00:00
浏览 31
已采纳

php表单一直给我500错误

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.

Here's my HTML form

        <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

<?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("","
"),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
";
        $email_content .= "Email: $email

";
        $email_content .= "Message:
$message
";


        $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!

  • 写回答

1条回答 默认 最新

  • doulao7998636570 2015-01-19 00:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C语言设计一个简单的自动换档程序
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。