dsx666666 2017-02-02 21:54
浏览 128

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

I downloaded some files from Bootstrapious to make a php form work. I have it send it e-mails, but no matter what I do it redirects to the php file which comes out as a blank page with the success message (no format, 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]: $value
";
    }
}

$headers = array('Content-Type: text/html; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
);

mail($sendTo, $subject, $emailText, implode("
", $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;
    }
})
});
  • 写回答

1条回答

  • douluan5738 2017-02-10 00:14
    关注

    Solved it. I wasn't calling the validator.js nor the contact.js correctly in the index.html body. Duh. Sorry to bother!

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题