从Wordpress发送AJAX联系表单时清空电子邮件数据(Empty email data when I send an AJAX contact form from Wordpress)

编程入门 行业动态 更新时间:2024-10-11 19:18:05
从Wordpress发送AJAX联系表单时清空电子邮件数据(Empty email data when I send an AJAX contact form from Wordpress)

我是一个使用AJAX和PHP开发的菜鸟。

我在Wordpress自定义主题中制作了这个HTML联系表单:

<div class="footer-form"> <form class="footer-form-contact" name="contact"> <h3 class="anuncio" id="formulario-contacto">Contacta con nosotros</h3> <input type="text" placeholder="Nombre" name="nombre" id="nombre" required/> <input type="email" name="email" id="email" placeholder="Email"/> <input type="text" placeholder="Teléfono" name="telefono" id="telefono" required/> <textarea name="mensaje" id="mensaje" placeholder="Escribe tu mensaje" required> </textarea> <input type="checkbox" required name="privacidad" value="privacidad"/> <input type="button" id="enviar" value="Enviar" onclick="cargaSendMail()"/> </form> <div id="c_information" class="hide"> <p></p> </div>

然后我按照这个问题文件路径为AJAX脚本(在Wordpress中)从admin-ajax.php获取url路径,如RRikesh do。

这是functions.js:

function cargaSendMail(){ $("#enviar").attr("disabled", true); $(".c_error").remove(); var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/; var s_email = $('#email').val(); var s_name = $('#nombre').val(); var s_telefono = $('#telefono').val(); var s_msg = $('#mensaje').val(); if (filter.test(s_email)){ sendMail = "true"; } else{ $('#email').after("<span class='c_error' id='c_error_mail'>Ingrese e-mail válido.</span>"); //aplicamos color de borde si el se encontro algun error en el envio $('.footer-form').css("border-color","#e74c3c"); sendMail = "false"; } if (s_name.length == 0 ){ $('#nombre').after( "<span class='c_error' id='c_error_name'>Ingrese su nombre.</span>" ); var sendMail = "false"; } if (s_telefono.length == 0 ){ $('#telefono').after( "<span class='c_error' id='c_error_telefono'>Ingrese su telefono.</span>" ); var sendMail = "false"; } if (s_msg.length == 0 ){ $('#mensaje').after( "<span class='c_error' id='c_error_msg'>Ingrese mensaje.</span>" ); var sendMail = "false"; } if(sendMail == "true"){ var datos = { "nombre" : $('#nombre').val(), "email" : $('#email').val(), "telefono" : $('#telefono').val(), "mensaje" : $('#mensaje').val() }; $.ajax({ url: ajaxurl, type: 'POST', data: datos + '&action=sendcontacto', beforeSend: function () { //aplicamos color de borde si el envio es exitoso $('.footer-form').css("border-color","#25A25A"); $("#enviar").val("Enviando…"); }, success: function (response) { $('.footer-form-contact')[0].reset(); $("#enviar").val("Enviar"); $("#c_information p").html(response); $("#c_information").fadeIn('slow'); $("#enviar").removeAttr("disabled"); } }); } else{ $("#enviar").removeAttr("disabled"); } }

和我的function.php:

function send_my_contact(){ $mensaje = $_POST['mensaje']; $mensaje.= "\n----------------------\n"; $mensaje.= "\nDe: ". $_POST['nombre']; $mensaje.= "\nEmail: ". $_POST['email']; $mensaje.= "\nTelefono: ". $_POST['telefono']; $destino = "hola@fiuuu.es"; $remitente = $_POST['email']; $asunto = "Mensaje enviado desde Talent Institut por ".$_POST['nombre']; mail($destino,$asunto,$mensaje,"FROM: $remitente"); echo "Mensaje enviado. Gracias por conectarse con nosotros"; } add_action('wp_ajax_sendcontacto', 'send_my_contact'); add_action('wp_ajax_nopriv_sendcontacto', 'send_my_contact');

这一切都好,系统正确地发送给我电子邮件,但数据是空的:

---------------------- De: Email: Telefono:

我究竟做错了什么?

I'm a noob with AJAX and PHP development.

I have made this HTML contact form in Wordpress custom theme:

<div class="footer-form"> <form class="footer-form-contact" name="contact"> <h3 class="anuncio" id="formulario-contacto">Contacta con nosotros</h3> <input type="text" placeholder="Nombre" name="nombre" id="nombre" required/> <input type="email" name="email" id="email" placeholder="Email"/> <input type="text" placeholder="Teléfono" name="telefono" id="telefono" required/> <textarea name="mensaje" id="mensaje" placeholder="Escribe tu mensaje" required> </textarea> <input type="checkbox" required name="privacidad" value="privacidad"/> <input type="button" id="enviar" value="Enviar" onclick="cargaSendMail()"/> </form> <div id="c_information" class="hide"> <p></p> </div>

Then I followed this question File path for AJAX script (in Wordpress) to get url path from admin-ajax.php like RRikesh do.

And this is functions.js:

function cargaSendMail(){ $("#enviar").attr("disabled", true); $(".c_error").remove(); var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/; var s_email = $('#email').val(); var s_name = $('#nombre').val(); var s_telefono = $('#telefono').val(); var s_msg = $('#mensaje').val(); if (filter.test(s_email)){ sendMail = "true"; } else{ $('#email').after("<span class='c_error' id='c_error_mail'>Ingrese e-mail válido.</span>"); //aplicamos color de borde si el se encontro algun error en el envio $('.footer-form').css("border-color","#e74c3c"); sendMail = "false"; } if (s_name.length == 0 ){ $('#nombre').after( "<span class='c_error' id='c_error_name'>Ingrese su nombre.</span>" ); var sendMail = "false"; } if (s_telefono.length == 0 ){ $('#telefono').after( "<span class='c_error' id='c_error_telefono'>Ingrese su telefono.</span>" ); var sendMail = "false"; } if (s_msg.length == 0 ){ $('#mensaje').after( "<span class='c_error' id='c_error_msg'>Ingrese mensaje.</span>" ); var sendMail = "false"; } if(sendMail == "true"){ var datos = { "nombre" : $('#nombre').val(), "email" : $('#email').val(), "telefono" : $('#telefono').val(), "mensaje" : $('#mensaje').val() }; $.ajax({ url: ajaxurl, type: 'POST', data: datos + '&action=sendcontacto', beforeSend: function () { //aplicamos color de borde si el envio es exitoso $('.footer-form').css("border-color","#25A25A"); $("#enviar").val("Enviando…"); }, success: function (response) { $('.footer-form-contact')[0].reset(); $("#enviar").val("Enviar"); $("#c_information p").html(response); $("#c_information").fadeIn('slow'); $("#enviar").removeAttr("disabled"); } }); } else{ $("#enviar").removeAttr("disabled"); } }

and my function.php:

function send_my_contact(){ $mensaje = $_POST['mensaje']; $mensaje.= "\n----------------------\n"; $mensaje.= "\nDe: ". $_POST['nombre']; $mensaje.= "\nEmail: ". $_POST['email']; $mensaje.= "\nTelefono: ". $_POST['telefono']; $destino = "hola@fiuuu.es"; $remitente = $_POST['email']; $asunto = "Mensaje enviado desde Talent Institut por ".$_POST['nombre']; mail($destino,$asunto,$mensaje,"FROM: $remitente"); echo "Mensaje enviado. Gracias por conectarse con nosotros"; } add_action('wp_ajax_sendcontacto', 'send_my_contact'); add_action('wp_ajax_nopriv_sendcontacto', 'send_my_contact');

It is every thing ok, and the system send me the email correctly, but the data are empty:

---------------------- De: Email: Telefono:

What am I doing wrong?

最满意答案

我会将您的数据修改为以下内容,以便在数据中明确设置datos :

var datos = { "nombre" : $('#nombre').val(), "email" : $('#email').val(), "telefono" : $('#telefono').val(), "mensaje" : $('#mensaje').val(), "action": 'sendcontacto' }; $.ajax({ url: ajaxurl, type: 'POST', data: datos,

I would modify your data to the following, so that the action is explicitly set in datos:

var datos = { "nombre" : $('#nombre').val(), "email" : $('#email').val(), "telefono" : $('#telefono').val(), "mensaje" : $('#mensaje').val(), "action": 'sendcontacto' }; $.ajax({ url: ajaxurl, type: 'POST', data: datos,

更多推荐

本文发布于:2023-08-03 07:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   清空   电子邮件   数据   Wordpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!