dtgta48604 2014-09-01 19:14
浏览 113
已采纳

使用jquery.post()时$ _POST上的未定义索引

This is my PHP script:

<?php
    require_once 'PHPMailerAutoload.php';
    $mailApp = new PHPMailer();

    $name = $_POST['name'];
    $message = $_POST['message'];
    $email = $_POST['email'];

    $mailApp->From = $email;
    $mailApp->FromName = $name;
    $mailApp->Subject = 'Contacto de http://profile.cv.hsoto.me';
    $mailApp->Body = $message;
    $mailApp->AddAddress('hectorsoto@balamtech.com', 'Hector Soto');
    if($mailApp->Send()){
        echo '<div class="alert alert-success text-center">Su mensaje fue enviado. Gracias por ponerse en contacto conmigo.</div>';
    } else {
        echo '<div class="alert alert-danger text-center">El mensaje no se pudo enviar. Por favor intente de nuevo o mándelo al <a href="mailto:hectorsoto@balamtech.com">hacer click aquí</a>.</div>';
    }

?>

Pretty straightforward, it sends an email.

This is my jquery script:

$('#cmdSendMessage').on('click', function(e){
        e.preventDefault();
        var message, name, email, error = "";
        email = $('#uemail').val();
        message = $('#umessage').val();
        name = $('#uname').val();
        if(email == "" || message == "" || name == ""){
            error = '<div class="alert alert-danger text-center">Uno o más campos están vacíos.</div>';
            $('#error-spot').append(error);
        }else if(!validateEmail(email)){
            error = '<div class="alert alert-danger text-center">El correo no es válido, su formato es incorrecto.</div>';
            $('#error-spot').append(error);
        } else{
            $.post('sendMail.php', 
                   {name : name, message : message, email : email}
                  ).done(
                        function(data){
                        $('#error-spot').append(data); 
                    }).fail(
                        function(data){
                        $('#error-spot').append('<div class="alert alert-danger text-center">Hubo un error y no se pudo mandar el correo. Hacerlo manualmente al <a href="mailto:hectorsoto@balamtech.com">hectorsoto@balamtech.com</a></div>');    
                    })
        }
    });

It processes data and sends the email, however, it thorws an error:

enter image description here

Any ideas why this might be happening?

EDIT:

When looking on the network tab on chrome, they are actually sent:

enter image description here

Which makes this even more weird that they are not being captured on the other side.

  • 写回答

3条回答 默认 最新

  • doutan1857 2014-09-01 19:59
    关注

    The answer to this question is pretty obvious, yet it happens to most of us at any given point on our programming life.

    else{
                $.post('sendMail.php', 
                       {name : name, message : message, email : email}
                      ).done(
                            function(data){
                            $('#error-spot').append(data); 
                        }).fail(
                            function(data){
                            $('#error-spot').append('<div class="alert alert-danger text-center">Hubo un error y no se pudo mandar el correo. Hacerlo manualmente al <a href="mailto:hectorsoto@balamtech.com">hectorsoto@balamtech.com</a></div>');    
                        })
            }
    

    The $.post shorthand method is easily a one line piece of code, the problem is that for readability, programmers tend to put it in many lines as the original post has it.

    After chaining the methods, some people often forget to put a semicolon right at the end of the final callback function.

    In this case, right at the end of the .fail() callback function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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