-6ren">
gpt4 book ai didi

javascript - 模式窗口上的 Php 表单 ajax “success & fail” 消息

转载 作者:行者123 更新时间:2023-11-30 17:13:37 24 4
gpt4 key购买 nike

我无法在同一模态窗口中显示错误消息或成功消息。我已经安装了 wordpress,我使用的代码如下:

表格

<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 (process.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/Ajax

$(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');
};
});
});
});

当我发送时,我重定向到表单“action”属性的页面,以及来自 proccess.php 的消息

你可以自己测试here .

这是修改的代码:How to Create an AJAX Contact Form .我认为问题在于帖子没有针对模态窗口进行解释。

最佳答案

您的页面上有一些 JS 错误。但是特别是这段代码

var formData, = $(form).serialize();

应该是

var formData = $(form).serialize();

关于javascript - 模式窗口上的 Php 表单 ajax “success & fail” 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26511376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com