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

使用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 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件