Error 500 (Internal Server Error) with PHP AJAX | Contact Form

0

I have created a contact form and every time I give it to send with all the fields filled , the following error appears:

HTML

<form id="contact-form" method="POST" action="">
<input type="text" id="form-name" name="form-name" placeholder="Nombre" required>
<input type="email" id="form-email" name="form-email" placeholder="Email" required>
<textarea id="form-message" name="form-message" placeholder="Mensaje" required></textarea>
<button type="submit" id="contact-btn" class="contact-btn btn">Enviar</button>
</form>

JQUERY | AJAX

$(function() {
    var form = $('#contact-form');
    var formMessages = $('#error-text');

    $(form).submit(function(event) {
        event.preventDefault();
        var formData = $(form).serialize();

        $.ajax({
            type: 'POST',
            url: 'include/php/contact.php',
            data: formData
        }).done(function(response) {
            $(formMessages).text(response);
            $('#error-alert').removeClass('closed');
            setTimeout(function(){ $('#error-alert').addClass('closed'); },5000);

            $('#form-name').val('');
            $('#form-email').val('');
            $('#form-message').val('');
        }).fail(function(data) {
            $(formMessages).text(response);
            $('#error-alert').removeClass('closed');
            setTimeout(function(){ $('#error-alert').addClass('closed'); },5000);

            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('<p>Oops! Ocurrió un error y tu mensaje no pudo ser enviado...</p>');
            }
        });
    });
});

PHP

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = strip_tags(trim($_POST["form-name"]));
    $name = str_replace(array("\r","\n"),array(" "," "),$name);
    $email = filter_var(trim($_POST["form-email"]), FILTER_SANITIZE_EMAIL);
    $message = trim($_POST["form-message"]);

    if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        http_response_code(400);
        echo "<p>Oops! Hubo un problema con tu envío. Por favor, completa el formulario e inténtalo otra vez...</p>";
        exit;
    }

    $recipient = "[email protected]";
    $subject = "Nuevo mensaje de $name";

    $email_content = "Nombre: $name\n";
    $email_content .= "Email: $email\n\n";
    $email_content .= "Mensaje:\n$message\n";

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

    if (mail($recipient, $subject, $email_content, $email_headers)) {
        http_response_code(200);
        echo "<p>¡Gracias! Tu mensaje ha sido enviado</p>";
    } else {
        http_response_code(500);
        echo "<p>Oops! Hubo un problema y no pudimos enviar tu mensaje. Por favor, inténtalo más tarde...</p>";
    }
} else {
    http_response_code(403);
    echo "<p>Hubo un problema con tu envío. Por favor, inténtalo otra vez...</p>";
}
?>

It says that the variable response is not defined ...

Thank you very much for your help

    
asked by Antonio 15.02.2017 в 21:32
source

1 answer

0

If the question is why the error occurs in your Javascript code Uncaught: ReferenceError: response is not defined is because within the callback function .fail response does not exist within the scope of the function, it would suffice that you replace response with data . ... }).fail(function(data) { $(formMessages).text(data); $('#error-alert').removeClass('closed'); ... Now if the question is why your PHP code returns an error 500 you should check if PHP can send emails, possibly because it is not properly configured. Also change the following line: $email_headers = "De: $name <$email>"; By: $email_headers = "From: $name <$email>"; because the headings must be English words.

I hope it helps you.

    
answered by 15.02.2017 / 23:25
source