在mac osx localhost MAMP上创建Jquery Ajax PHP邮件表单500错误

时间:2016-04-13 00:51:42

标签: mamp

我正在关注http://blog.teamtreehouse.com/create-ajax-contact-form的博文,我已经完成了对我的localhost / MAMP配置的所有步骤,除了让php脚本实际邮寄文件内容。它一直给我500错误,没有其他描述。我试图通过命令行获取错误输出,但它不会记录任何错误。 MAMP上的PHP版本是7.0.0 -

首先是php代码:

<?php

    // 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;
        }

        // Set the recipient email address.
        // FIXME: Update this to your desired email address.
        $recipient = "jeromemoore@usa.com";

        // Set the email subject.
        $subject = "New contact from $name";

        // Build the email content.
        $email_content = "Name: $name\n";
        $email_content .= "Email: $email\n\n";
        $email_content .= "Message:\n$message\n";

        // Build the email headers.
        $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! Your message has been sent.";
        } else {
            // Set a 500 (internal server error) response code.
            http_response_code(500);
            echo "Oops! Something went wrong and we couldn't send your message.";
        }

    } 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.";
    }

?>

Javascript:

<script>
        $(document).ready(function($) {
     var form = $('#ajax-contact');
    $(form).submit(function(event) {
         var formData = {
             'name'              : $('#name').val(),
             'email'             : $('#email').val(),
             'message'           : $('#message').val()
         };
        //var formData = $(form).serialize();
        event.preventDefault();
        console.log(formData);
        $.ajax({
            type: "POST",
            url: $(form).attr('action'), 
            data: formData,
            success: function(msg){
              alert("form post success!");
                //$("#stayInformed").modal('hide');
            },
            error: function(){
                alert("form post failed");
            }   
        });
    });
  });
</script>

表格:

<p class="holla">Contact Us</p>

<form method="post" id="ajax-contact" name="emailform" action="email.php">
  <input type="text" name="name" placeholder="Name" id="name" required>
  <input type="text" name="email" placeholder="Email" id="email" required>
  <textarea name="message" placeholder="Message" id="message" required></textarea>
  <button type="submit" name='submit' value="submit">Send</button>

</form>

任何帮助将不胜感激

0 个答案:

没有答案