jQuery ajax发布数据以形成邮件

时间:2014-05-15 18:53:48

标签: javascript php jquery ajax post

我试图让jQuery通过Ajax将一些数据发布到PHP表单邮件,因此我可以通过电子邮件发送数据。 但我的问题是它没有在邮件中发布任何内容。它是空的。

这是我的javascript:

$(document).ready(function() {
  $('#food_requirements').hide();
  $('#name_allergy').hide();
  $('#food').click(function(e) {
    $('#food_requirements').fadeIn('slow');
  });
  $('#allergy').click(function(e) {
    $('#name_allergy').fadeIn('slow');
  });

  $('.confirm input').click(function(e) {
    $(".confirm").removeClass('red-border');
  });
  $('.checkin').click(function() {
    $(this).attr("src", 'images/loader.gif');
    var checkBoxs = document.getElementsByName("confirmation");
    var confirmation = '';
    for (var i = 0; i < checkBoxs.length; i++) {
      if (checkBoxs[i].checked) {
        confirmation += checkBoxs[i].value + " - ";
      }
    }
    if (confirmation == '') {
      $('.confirm').addClass('red-border');
      $(this).attr("src", 'images/checkin.jpg');
      return false;
    }
    if (document.getElementById("food").checked) {
      var food = "yes";
    } else {
      var food = "no";
    }
    var food_requirements = $('#food_requirements').val();
    if (food == 'yes') {
      if (food_requirements == '') {
        $('#food_requirements').addClass('red-border');
        $('#food_requirements').focus();
        $(this).attr("src", 'images/checkin.jpg');
        return false;
      } else {
        $("#food_requirements").removeClass('red-border');
      }
    }
    if (document.getElementsByName("allergy").checked) {
      var allergy = "yes";
    } else {
      var allergy = "no";
    }
    var name_allergy = $('#name_allergy').val();
    if (allergy == 'yes') {
      if (name_allergy == '') {
        $('#name_allergy').addClass('red-border');
        $('#name_allergy').focus();
        $(this).attr("src", 'images/checkin.jpg');
        return false;
      } else {
        $("#name_allergy").removeClass('red-border');
      }
    }
    var firstname1 = $('#firstname1').val();
    if (firstname1 == '') {
      $('#firstname1').addClass('red-border');
      $('#firstname1').focus();
      $(this).attr("src", 'images/checkin.jpg');
      return false;
    } else {
      $("#firstname1").removeClass('red-border');
    }
    var lastname1 = $('#lastname1').val();
    if (lastname1 == '') {
      $('#lastname1').addClass('red-border');
      $('#lastname1').focus();
      $(this).attr("src", 'images/checkin.jpg');
      return false;
    } else {
      $("#lastname1").removeClass('red-border');
    }
    var age1 = $('#age1').val();
    if (age1 == '') {
      $('#age1').addClass('red-border');
      $('#age1').focus();
      $(this).attr("src", 'images/checkin.jpg');
      return false;
    } else {
      $("#age1").removeClass('red-border');
    }
    var fname = "";
    var firstname = document.getElementsByName("firstname");
    for (var i = 0; i < firstname.length; i++) {
      if (firstname[i].value != '') {
        if (firstname[i].value != '') fname += firstname[i].value + " / ";
      }
    }
    var lname = "";
    var lastname = document.getElementsByName("lastname");
    for (var i = 0; i < lastname.length; i++) {
      if (lastname[i].value != '') lname += lastname[i].value + " / ";
    }
    var ages = "";
    var age = document.getElementsByName("age");
    for (var i = 0; i < age.length; i++) {
      if (age[i].value != '') ages += age[i].value + " / ";
    }










    var data = "&confirmation=Events: " + confirmation + "&firstname=First names: " + fname + "&lastname=Last names: " + lname + "&age=Ages: " + ages;
        if (food_requirements != '') data = data + "&food_requirements=Food requirements: " + food_requirements;
        if (name_allergy != '') data = data + "&allergy=Allergy: " + name_allergy;
        var url = "rsvp.php";
        $('#form').html('Your CHECK-IN is done!.<br/> Thank you very much.');
        $.post(url, data, function(msg) {
          $('#form').addClass('formfinal');
            alert(data)
          window.scrollTo(0, 0);
          if (msg.substr(0, 2) == "ok") {
            $('#form').html('Your CHECK-IN is done!.<br/> Thank you very much.');
          } else {
            $('#form').html('There has been an error with the CHECK-IN. Please try again.');
            return false;
          }
        });





  });
});
function isEmail(email) {
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}
function addguest() {
  $("#guests").append("<input class='firstname' name='firstname' placeholder='First Name' /><input class='lastname' name='lastname' placeholder='Last Name' /><input class='age' name='age' placeholder='Age' /><div class='clr'></div>");
}

当我提醒&#34;数据&#34;

时,我得到了这个值

&#34;&amp; confirmation =事件:宝莱坞 - &amp; firstname =名字:andreas / john / james /&amp; lastname =姓氏:Johnson / Jamesson / Hansen /&amp; age =年龄:24/26 / 42 /&amp; food_requirements =食品要求:是否有效&#34;

这是我的php(rsvp.php)表单。我尝试了一些不同的东西只是为了获得某种价值,但没有任何运气。我收到一封空电子邮件。

<?php
   $email = 'from@mail.com';
   $message = 'firstname' . $firstname . '';
   $to = 'me@mail.com';
   mail($to, 'my subject', $message, $email);
?>

请让我知道该怎么做。

1 个答案:

答案 0 :(得分:1)

问题在这里 你期待$ firstname = $ _POST [&#39; fname&#39;];但是传递名字

var data = "&confirmation=Events: " + confirmation + "&firstname=First names: " + fname + "&lastname=Last names: " + lname + "&age=Ages: " + ages;