2012-08-30 89 views
-1

我正在學習PHP,並有一個問題,我希望有一個簡單的答案。我在過去創建了基本表單,並在提交時將相應的電子郵件發回給用戶。發送電子郵件基於哪個複選框被選中

這一次,我有3個複選框。根據選擇哪個複選框,我需要發送給他們的某個電子郵件。

例如,如果他們希望接收文檔1和3,然後提交,則電子郵件會向他們發送鏈接以下載這兩個文檔,依此類推。

我不介意它是否發送兩封電子郵件,每個複選框選擇一個。如果他們選擇兩個他們希望收到的文件,他們會得到一個電子郵件鏈接到文件1,另一個文件3。

我不知道我是否可以甚至用PHP做到這一點。

+0

是的,你可以用PHP來做到這一點。你有什麼嘗試? –

回答

0
// create an array with the checkboxes so you can later easily expand it 
// the indexes of the array are the fieldnames of the checkboxes 
$checkboxes = array(
    'checkbox1' => 'http://example.com/somelink', 
    'checkbox2' => 'http://example.com/somelink2', 
    'checkbox3' => 'http://example.com/somelink3', 
); 

// generate mail body based on checked checkboxes 
$mailContents = 'Your links:'; 
foreach($checkboxes as $name => $link) { 
    if (isset($_POST[$name])) { 
     $mailContents.= $link . "\n"; 
    } 
} 

現在您可以將包含在$mailContents中的鏈接郵寄爲字符串。

0

設置一個值,每複選框,然後使用POST或GET方法發送的三個複選框的值,然後你做:

if(check1 == "value"){ 
    //send email 1 
} 
if(check2 == "value"){ 
    //send email 2 
} 
if(check3 == "value"){ 
    //send email 3 
} 
0

你需要做的是以下幾點: 創建複選框是這樣的:

<input type="checkbox" name="sendDoc1" value="Selected" /> Send me Document 1 

然後當PHP的檢查表,您可以使用isset和檢查輸入

if(isset($_POST['sendDoc1']) && 
$_POST['sendDoc1'] == 'Selected') 
{ 
    //Code to send email 
} 
012的內容

,你可以重複,對於每個文檔

0

創建文檔複選框形式

<form method="post" action="test.php"> 
    Document 1: <input type="checkbox" name="document[]" value="document1" /><br /> 
    Document 2: <input type="checkbox" name="document[]" value="document2" /><br /> 
    Document 3: <input type="checkbox" name="document[]" value="document3" /><br /> 
    <br /> 
    <input type="hidden" name="send" value="1" /> 
    <input type="submit" value="Send" /> 
</form> 

和PHP腳本來處理它

<?php 
    if(isset($_POST) && ($_POST['send'] == 1)){ 

     $documents = array(
         'document1' => 'http://www.example.com/document1.doc', 
         'document2' => 'http://www.example.com/document2.doc', 
         'document3' => 'http://www.example.com/document3.doc' 
        ); 

     $to  = '[email protected]'; 
     $subject = 'the subject'; 
     $message = "hello\n\n"; 

     if(isset($_POST['document']) && count($_POST['document']) > 0){ 
      foreach($_POST['document'] as $doc){ 
       if(isset($documents[$doc])){ 
        $message .= "Here is ".$documents[$doc]."\n"; 
       } 
      } 
     } 


     $headers = 'From: [email protected]' . "\r\n" . 
      'Reply-To: [email protected]' . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 

     mail($to, $subject, $message, $headers); 
    } 
?> 

我覺得你得到了總體思路。

+0

謝謝!正是我需要的。 – dkype

相關問題