2010-06-03 94 views
1

我有一個表單需要運行一個php腳本,一旦點擊提交按鈕,它需要是ajax。需要在提交表單時運行php腳本

<form method="post" action="index.php" id="entryform" name="entryform"> 
<input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ee_web/include/email-notification.php', 'entryform')" /> 
</form> 

在這種情況下,使用if(表單提交){得到的結果和運行腳本}是不是一種選擇,我需要外部運行腳本,這就是爲什麼我需要它來執行電子郵件通知。 php onclick

當我將我的網頁瀏覽器指向domain.com/include/email-notification.php時,它運行完美。

任何幫助表示讚賞。

這會在提交表單之前執行腳本,現在我需要等待一旦點擊提交按鈕來執行腳本,可以做到這一點?

$.ajax({ 
    type: "POST", 
    url: "/include/email-notification.php", 
    dataType: "script" 
}); 
+0

我想你應該把它指向/include/email-notification.php在表單中呢? – baloo 2010-06-03 15:06:37

+0

是的,我想知道同樣的事情。布拉德,是'/ web/ee_web'部分應該在那裏? – KyleFarris 2010-06-03 15:09:56

+0

/web/ee_web /是絕對路徑 – Brad 2010-06-03 15:12:44

回答

0

試着改變你的 「點擊」 值:

<input type="submit" name="submit" value="Submit" onclick="JavaScript:xmlhttpPost('/web/ee_web/include/email-notification.php', 'entryform'); return false" /> 

這是假設的 「xmlhttpPost()」 的東西,實際工作自身。

1

我建議使用jQuery。由於其靈活性和conveinience

如果您想發送電子郵件通知,然後只發布的數據,那麼請執行下列操作:

創建一個單獨的頁面來處理像email.php

// fill in headers and prepare content 
$result = mail(............................); 
if($result) { return 1; } else { return 0; } 

您的電子郵件通知然後在你的表格上

$('#mysubmitbutton').click(function() { 
     $.post(
      'email.php', 
      { emailadd: '[email protected]', content: 'mycontent' }, //send parameters to email.php 
      function(data) { 
       //now check whether mail sending was successfull 
       if(data==1) { 
        //message sending was successful so carry out further operation 
        //................................. 
       } 
       else { alert("Sorry, email notification was unsuccessfull"); } 
      }); 
});