PHP表单AJAX“的成功和放大器;失败“的模态窗口消息 [英] Php form ajax “success & fail” message on modal window
问题描述
我不能在同一个模态窗口中的错误信息或成功。 我已经安装了字preSS和code我使用的是如下:
I can not make the error message or success in the same modal window. I have installed wordpress and the code I'm using is as follows:
表格
<form id="formcotizador" method="post" action="<?php echo get_template_directory_uri(); ?>/cotizador/proccess.php">
<div class="small-12 medium-4 large-4 columns">
<input id="nombre" type="text" name="nombre" placeholder="Nombre y Apellido" required>
</div>
<div class="small-12 medium-4 large-4 columns">
<select name="provincia" id="provincia">
<option value="default">Seleccione una Provincia</option>
<option value="Buenos Aires">Buenos Aires</option>
<!-- ... -->
</select>
</div>
<div class="small-12 medium-4 large-4 columns">
<input type="text" id="localidad" name="localidad" placeholder="Localidad" required>
</div>
<div class="small-12 medium-4 large-4 columns">
<input type="email" id="email" name="email" placeholder="Tu email" required>
</div>
<div class="small-12 medium-4 large-4 columns">
<input type="submit" value="Enviar consulta" class="button tiny radius">
</div>
<!-- error, success div -->
<div class="small-12 medium-4 large-4 columns msjj"></div>
</form>
PHP (proccess.php)
PHP (proccess.php)
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = strip_tags(trim($_POST['nombre']));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$provincia = $_POST['provincia'];
$localidad = strip_tags(trim($_POST['localidad']));
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$telefono = strip_tags(trim($_POST['telefono']));
$recipient = "produccion@dev.criterionet.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>";
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "Gracias por enviar el msj";
} else {
http_response_code(500);
echo "Hubo un error al enviar el msj";
}
} else {
http_response_code(403);
echo "hubo un problema al enviar, pruebe de nuevo";
}
?>
JS /阿贾克斯
$(function() {
var form = $('#formcotizador');
var formMessages = $('.msjj');
$(form).submit(function(e) {
e.preventDefault();
var formData, = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData,
})
.done(function(response) {
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
$(formMessages).text(response);
$('#nombre').val('');
$('#provincia').val('default');
$('#localidad').val('');
$('#email').val('');
})
.fail(function(data) {
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Este es el error');
};
});
});
});
当我送,我重定向到形式'行动'的属性页,从proccess.php
When I send, I redirected to the page of the form 'action' attribute, with the message from the proccess.php
您可以测试自己这里。
这是在code表示修改:如何创建一个AJAX触点形式。 我认为这个问题是该职位没有解释不是一个模态窗口。
This is the code that modifies: How to Create an AJAX Contact Form. I think the problem is that the post is not explained not for a modal window.
推荐答案
您有您的网页上几个JS错误。但与此code特别
You have a few JS errors on your page. But with this code in particular
var formData, = $(form).serialize();
应
var formData = $(form).serialize();
这篇关于PHP表单AJAX“的成功和放大器;失败“的模态窗口消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!