2013-03-16 16 views
3

在這個例子中,我使用的是MySQL,我知道它已被棄用,但這只是爲了學習目的。我也在學習如何使用PDO,現在只是不想因PDO的缺乏經驗而陷入困境,所以我現在正在使用mysql。函數檢查數據庫,返回消息,併發送電子郵件

好吧,所以我有一個jQuery AJAX函數提交表單數據到PHP函數頁面。 PHP頁面然後與數據庫進行通信並返回結果。所有這些工作到目前爲止。

用戶填寫表單提供他們的電子郵件地址。電子郵件傳遞給PHP頁面並輸入到數據庫中。如果用戶存在,則會顯示一條消息,告訴他們他們已經訂閱。如果不存在,則添加它們,然後一條消息告訴它們已成功添加。好消息是所有這些都很棒!

現在,我有一個問題,在同一個回調函數「dispAdd」期間,我想生成一個自動的歡迎電子郵件給用戶。不管我怎麼嘗試編碼郵件通話,我似乎都在函數中出錯。我會給你我現在所擁有的東西,但是如果有人能夠提供幫助,我將非常感激。

這裏是我的回調函數,因爲所有其他部件工作正常現在:

function dispAdd()    // Serves as callback function to jQuery/AJAX in contact.html 
{ 
    $sql= "SELECT * FROM mailList WHERE email = '$email'"; 
    $result= mysql_query($sql) or die(mysql_error()); 
    $to = "[email protected]"; 
    $who = "ME"; 
    $message = "WOW"; 
    $subject = "TESTING"; 
    $message = $who . ', ' . $message; 
    $headers = "From: [email protected]" . "\r\n"; 

    if(mysql_num_rows($result) > 0) // Checks to see if query returns any info for the calling function 
    { 
    mail($to,$subject,$message,$headers); 
    while ($row = mysql_fetch_assoc($result)) 
    return; 
    } 
} 

原始呼叫:

$('#contForm').submit(function() { 
    var formData = $(this).serialize();   // Stores all form data in AJAX variable 
    $.post('contact.php',formData,dispAdd); 
    function dispAdd(result) {    // Callback function requests result of RESULT 
    if (!result) { 
     $('#main').html('<div>Your have been added to the mailing list</div>'); 
    } else { 
     if ($('#fail').length==0) { 
     $('#main').append('<div id="fail" style="color:red";>This email address is already subscribed to our mailing list</div>'); 
     } 
    } 
    } 
    return false; // Stops form from loading contact.php page 
}); 
+0

也許你也可以發佈錯誤? – fredrik 2013-03-16 17:35:06

+0

我重新格式化時發生的唯一錯誤是有時候我的返回不起作用。 – 2013-03-16 17:36:31

回答

1
<script> 
    $('#contForm').submit(function() 
    { 
    var formData = $(this).serialize();   // Stores all form data in AJAX variable 
    $.post('contact.php',formData, function(data) 
    { 
     console.log(data); 
     if(data) 
     { 
     $('#main').html('<div>You have been added to the mailing list</div>'); 
     } 
     else 
     { 
     $('#main').append('<div id="fail" style="color:red";>This email address is already subscribed to our mailing list</div>'); 
     } 
     console.log(data); 
    }); 
    return false; // Stops form from loading contact.php page 
    }); 
</script> 

然後,在contact.php你應該把下面的功能:

function dispAdd()    // Serves as callback function to jQuery/AJAX in contact.html 
{ 
    $sql= "SELECT * FROM mailList WHERE email = '$email'"; 
    $result= mysql_query($sql) or die(mysql_error()); 
    $to = "[email protected]"; 
    $who = "ME"; 
    $message = "WOW"; 
    $subject = "TESTING"; 
    $message = $who . ', ' . $message; 
    $headers = "From: [email protected]" . "\r\n"; 

    if(mysql_num_rows($result) > 0) // Checks to see if query returns any info for the calling function 
    { 
    mail($to,$subject,$message,$headers); 
    while ($row = mysql_fetch_assoc($result)) 
    return; 
    } 
} 

在contact.php上的代碼運行後,讓它調用dispAdd函數並將dispAdd的結果返回到您的Ajax請求。

+0

我正在使用數據庫,只是沒有這個例子....你看到的是我IWLL正在使用的一個變化,這就是爲什麼我重新寫入TO。 – 2013-03-16 17:42:37

+0

是的郵件設置和工作正常與其他頁面完全在這裏 – 2013-03-16 17:42:57

+0

我已經添加了「if,else」語句以確保您(認爲)被調用的代碼實際上被調用。如果您認爲這是可能的,則查詢不會返回任何用戶。如果是這樣的話,查詢可能會有一些小問題。 echo語句只是讓你知道代碼在哪裏。 – 2013-03-16 17:47:46

相關問題