2012-07-06 29 views
0

我尝试确认电子邮件是使用JSON发送的,然后使用ajax发送事件并在屏幕上显示成功消息。 问题是电子邮件没有发送,并且由于php脚本的第一部分,我被重定向到“sended”:true。 HTML和CSS是好的,我已经测试了很多。 我真正想要的是用php发送邮件,而不是使用ajax来显示成功或错误消息。问题是我这是我第一次使用php,我无法找到正确的方法。我不会问这个,但是我必须在今晚完成这个项目,这是最后要做的事情。 您可以通过电子邮件我们的链接在http://webofdreams.ro/vworker/finemaid/finemaid.html#检查它的行动。json succes消息shawn

JS:

$ajax({ 
        "type":"POST", 
        "url":"sendemail1.php", 
        "data": { name1: name1Val, emailFrom1: emailFrom1Val, comments: commentsVal}, 
        "dataType":'json', 
        "success":function(response){ 
         if (response.sended){ 
          alert ("Mail Sended ok"); //Code after mail send 
          }else{ 
          alert (response.error); //Code or allert on error 
          } 
          },     
        error: function (xhr, ajaxOptions, thrownError){ 
         alert(xhr.status+" "+thrownError); 
        } 

PHP:

$send = @mail($mailTo, $subject, $message, "From: ".$mailFrom1); 
if ($send){ 
    echo mail($mailTo, $subject, $message, "From: ".$mailFrom1) ? '{"sended":true}':'{"sended":false,"error":"Mail send fail."}'; 
}else{ 
echo '{"sended":false,"error":"Request Error."}'; 
}; 
+1

您遇到的问题,发送电子邮件,或只是显示成功的消息与Ajax是@viktor你要明白,这个脚本会发送两封电子邮件,唯一的问题 – 2012-07-06 08:55:39

+0

不是吗? – Teneff 2012-07-06 08:58:21

+0

其实它是$ .ajax不是$ ajax,为firefox安装firebug它会告诉你你在jquery代码中有错误... – 2012-07-06 08:59:45

回答

1

你的Ajax请求是完全错误的,所以我已经写了它从头开始,下面的脚本将至少照顾ajax请求。

 $(function(){ 
     name1Val = 'CaptureName'; 
     emailFrom1Val = 'CaptureEmail'; 
     commentsVal = 'CaptureComments'; 
     parameters = 'name1=' + name1Val + '&emailFrom1=' + emailFrom1Val + '&comments=' + commentsVal; 
     $.ajax({ 
      type: "POST", 
      url: "sendmail.php", 
      data: parameters, 
      error: function(response){ 
       alert(response); 
      }, 
      success: function(response){ 
       alert(response); 
      } 
     }); 
    }); 
+0

除了定义变量,我没有看到重大差异。这个问题几乎可以肯定的在PHP代码中。 – viktor 2012-07-06 09:32:17

1

首先我在你的代码发现错误

$.ajax({ 
^     "type":"POST", 
|     "url":"sendemail1.php", 
|     "data": { name1: name1Val, emailFrom1: emailFrom1Val, comments: commentsVal}, 
        "dataType":'json', 
        "success":function(response){ 
         if (response.sended){ 
          alert ("Mail Sended ok"); //Code after mail send 
          }else{ 
          alert (response.error); //Code or allert on error 
          } 
          }, 

     ----------->error: function (xhr, ajaxOptions, thrownError){ 
         alert(xhr.status+" "+thrownError); 
        // should be 
     ----------->"error": function (xhr, ajaxOptions, thrownError){ 
         alert(xhr.status+" "+thrownError); 

        } 
+0

迈克尔也需要$ .ajax – 2012-07-06 09:01:34

+0

Jup。我编辑了它;-) – Tschallacka 2012-07-06 09:03:13