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

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

This is my PHP script:

  1. <?php
  2. require_once 'PHPMailerAutoload.php';
  3. $mailApp = new PHPMailer();
  4. $name = $_POST['name'];
  5. $message = $_POST['message'];
  6. $email = $_POST['email'];
  7. $mailApp->From = $email;
  8. $mailApp->FromName = $name;
  9. $mailApp->Subject = 'Contacto de http://profile.cv.hsoto.me';
  10. $mailApp->Body = $message;
  11. $mailApp->AddAddress('hectorsoto@balamtech.com', 'Hector Soto');
  12. if($mailApp->Send()){
  13. echo '<div class="alert alert-success text-center">Su mensaje fue enviado. Gracias por ponerse en contacto conmigo.</div>';
  14. } else {
  15. 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>';
  16. }
  17. ?>

Pretty straightforward, it sends an email.

This is my jquery script:

  1. $('#cmdSendMessage').on('click', function(e){
  2. e.preventDefault();
  3. var message, name, email, error = "";
  4. email = $('#uemail').val();
  5. message = $('#umessage').val();
  6. name = $('#uname').val();
  7. if(email == "" || message == "" || name == ""){
  8. error = '<div class="alert alert-danger text-center">Uno o más campos están vacíos.</div>';
  9. $('#error-spot').append(error);
  10. }else if(!validateEmail(email)){
  11. error = '<div class="alert alert-danger text-center">El correo no es válido, su formato es incorrecto.</div>';
  12. $('#error-spot').append(error);
  13. } else{
  14. $.post('sendMail.php',
  15. {name : name, message : message, email : email}
  16. ).done(
  17. function(data){
  18. $('#error-spot').append(data);
  19. }).fail(
  20. function(data){
  21. $('#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>');
  22. })
  23. }
  24. });

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 11:59
    关注

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

    1. else{
    2. $.post('sendMail.php',
    3. {name : name, message : message, email : email}
    4. ).done(
    5. function(data){
    6. $('#error-spot').append(data);
    7. }).fail(
    8. function(data){
    9. $('#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>');
    10. })
    11. }

    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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部