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条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?