Ajax表示成功的消息,但不是电子邮件。

时间:2013-09-11 10:28:59

标签: php javascript ajax submit

嗨我有一个“发送给朋友”的PHP,如果你使用正常的PHP帖子工作得很好,这种方式我知道这个PHP可以发送邮件..所以问题不是sendtomail.php我也会发布在底部..这里是代码

/* Código AJAX Send to Friend*/
$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".enterrenvi").click(function() {
        // validate and process form
        // first hide any error messages
    $('.error').hide();

      var youremailaddress = $("input#youremailaddress").val();
        if (youremailaddress == "") {
      $("label#youremailaddress_error").show();
      $("input#youremailaddress").focus();
      return false;
    }
        var friendsemailaddress = $("input#friendsemailaddress").val();
        if (friendsemailaddress == "") {
      $("label#friendsemailaddress_error").show();
      $("input#friendsemailaddress").focus();
      return false;
    }


        var dataString = 'youremailaddress='+ youremailaddress + '&friendsemailaddress=' + friendsemailaddress;
        //alert (dataString);return false;

        $.ajax({
      type: "POST",
      url: "sendtofriend.php",
      data: dataString,
      success: function() {
        $('#message').html("<div id='messagein'></div>");
        $('#messagein').html("<h2>Contact Form Submitted!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#messagein').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
    });
});
$(document).ready(function(){
  $("input#youremailaddress").select().focus();
});

Sendtofriend.php

<?PHP
$uemail = $_POST["youremailaddress"]; 
$femail = $_POST["friendsemailaddress"];
$message = "myfootballproject.com";
$link = $_SERVER['HTTP_REFERER'];

$to = "$femail";
$subject = "Tu amigo $uemail te invita a conocer My Football Project";
$headers = "From: $uemail\n";
$message = "Hi $femail.  Te invitamos a conocer My Football Project.
$link
$message";

if (mail($to,$subject,$message,$headers) ){ echo "se envío el correo"; }
else { echo "fallo el envío";};


?>

形式:

<div id='message'>
    <div id='messagein'></div>
<form action="" method="post" id="sendfriendd">

<div id="inpumail" >

    <input type="text" name="youremailaddress" id="youremailaddress" size="40" value="" class="text-input" />  
    <label class="error" for="youremailaddress" id="youremailaddress_error">This field is required.</label>  

</div>


<br>
<div id="inpumail2" >


    <input type="text" name="friendsemailaddress" id="friendsemailaddress" size="40" value="<%= t('generales.amigcorreoo') %>" class="text-input" />  
    <label class="error" for="friendsemailaddress" id="friendsemailaddress_error">This field is required.</label>

</div>


<br>
<input type="submit" name="Submit" value=" <%= t('generales.enviarcorreoo') %> " class="enterrenvi">
</form>
</div>

1 个答案:

答案 0 :(得分:0)

您尝试使用POST参数发送GET数据。

在这里查看dataString。它得到了:

    var dataString = 'youremailaddress='+ youremailaddress + '&friendsemailaddress=' + friendsemailaddress;

但实际上你提出POST请求:

    $.ajax({
         type: "POST",

要解决此问题,请使用$_GET,或继续使用$_POST jQuery的$.post方法。不过,我强烈建议在这种情况下使用POST。

另外,尽量不要依赖PHP的mail()方法。使用SMTP代替类似Swiftmailer的类。