PHP 联系表单重定向到联系人.php并显示成功消息



我从Bootstrapious下载了一些文件以使php表单工作。我让它发送电子邮件,但无论我做什么,它都会重定向到 php 文件,该文件显示为带有成功消息的空白页(无格式,tho)。

.HTML

<form id="contact-form" method="post" action="../prueba/assets/php/contact.php">
<div class="messages"></div>
<div class="controls">
<div class="form-group">
<label for="name">Nombre</label>
<input id="name" type="text" name="name" class="form-control" placeholder="Escribe tu nombre" required data-error="Por favor, escribe tu nombre.">
<label for="email">Email</label>
<input id="email" type="email" name="email" class="form-control" placeholder="Escribe tu e-mail" required data-error="Por favor, escribe tu e-mail.">
<label for="comment">Mensaje</label>
<textarea id="comment" name="comment" class="form-control" rows="3" required data-error="Por favor, escribe tu mensaje."></textarea>
<div class="help-block with-errors"></div>
</div>
<button type="submit" class="btn btn-default">Enviar <span class="glyphicon glyphicon-send"></button>
</div>

.PHP

<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// configure
$from = 'Con Ilan a la India <conilanalaindia@gmail.com>';
$sendTo = 'andreaisyourfriend@gmail.com';
$subject = 'Nuevo mensaje del formulario';
$fields = array();
$fields["name"] = "Nombre";
$fields["email"] = "E-mail";
$fields["comment"] = "Message"; // array variable name => Text to appear in the email
$okMessage = 'El formulario ha sido enviado correctamente. Te contactaremos dentro de poco.';
$errorMessage = 'Ha ocurrido un error al enviar el formulario. Por favor, inténtalo nuevamente.';
// let's do the sending
try
{
$emailText = "Hay un nuevo mensaje de la web Con Ilan a la India";
foreach ($_POST as $key => $value) {
    if (isset($fields[$key])) {
        $emailText .= "$fields[$key]: $valuen";
    }
}
$headers = array('Content-Type: text/html; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}

.JS

$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
    if (!e.isDefaultPrevented()) {
        var url = "../prueba/assets/php/contact.php";
        $.ajax({
            type: "POST",
            url: url,
            data: $(this).serialize(),
            success: function (data)
            {
                var messageAlert = 'alert-' + data.type;
                var messageText = data.message;
                var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                if (messageAlert && messageText) {
                    $('#contact-form').find('.messages').html(alertBox);
                    $('#contact-form')[0].reset();
                }
            }
        });
        return false;
    }
})
});

解决了。我没有在索引正文中正确调用验证者.js也没有.js联系人.html。咄。抱歉打扰!

最新更新