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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动