doufu3718 2015-06-17 14:37
浏览 67
已采纳

从Wordpress发送AJAX联系表单时清空电子邮件数据

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.= "
----------------------
";
$mensaje.= "
De: ". $_POST['nombre'];
$mensaje.= "
Email: ". $_POST['email'];
$mensaje.= "
Telefono: ". $_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?

  • 写回答

1条回答 默认 最新

  • du3669 2015-06-17 14:39
    关注

    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,
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名