2012-07-06 26 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