2011-04-18 64 views
1

嗨,我是新的PHP 如何將郵件發送到PHP中所選城市與缺乏自信的電子郵件ID與$主題$消息$頭是用PHP switch語句這個正確的方式

$selectcity = array("Bengaluru","Coimbatore","Chennai");                           
switch(trim($selectcity)) 
{ 
     case "Bengaluru": 
     { 
     $to = '[email protected]''$subject''$message''$headers'; 
     break; 
     } 
     case "Coimbatore": 
     { 
     $to = '[email protected]''$subject''$message''$headers'; 
       break; 
     } 
     case "Chennai": 
     { 
     $to = '[email protected]''$subject''$message''$headers'; 
       break; 
     }  
    } 

回答

1

裝飾()不支持數組。在switch語句中,應該放置一個選定的結果,而不是可用的選項。

參考:http://php.net/manual/en/function.trim.php

例子:

假設你有一個表格發送選擇選項(例如下拉表單中箱):

$selected = trim($_POST['ddb_city']); 
switch($selected) { 
    // the cases 
} 

此外,發送電子郵件,檢查PHP(http://php.net/manual/en/function.mail.php)中的郵件()函數。你當前的語法是錯誤的。

+0

ü可以給我一些例子,說明如何做到這一點???我迫切需要添加 – Abhijeet 2011-04-18 03:29:12

+0

示例。 – Raptor 2011-04-18 03:39:27

1

您無法打開陣列。你需要有一個特定的值來打開,就像數組中的一個元素一樣。

$selectcity = array("Bengaluru","Coimbatore","Chennai");                           
$i=0; // this doesn't really make sense in the context, just used for the example 
switch(trim($selectcity[$i])) 
{ 
    case "Bengaluru": 
     $to = '[email protected]'; 
     $subject="subject"; 
     $message="message"; 
     $headers="test"; 
     break; 

    case "Coimbatore": 
     $to = '[email protected]'; 
     $subject="subject"; 
     $message="message"; 
     $headers="test"; 
     break; 
    case "Chennai": 
     $to = '[email protected]'; 
     $subject="subject"; 
     $message="message"; 
     $headers="test"; 
     break; 
    default: 
     $to = '[email protected]'; 
     $subject="subject"; 
     $message="message"; 
     $headers="test"; 
} 
+0

嘿,我試過你說,但它給錯誤這樣的解析錯誤:語法錯誤,意想不到的T_CONSTANT_ENCAPSED_STRING第6行中的示例代碼 – Abhijeet 2011-04-18 03:41:11

+0

多數民衆贊成多數民衆贊成,因爲你的價值引用是在你的'$到'值搞砸了。 – prodigitalson 2011-04-18 03:43:29

+0

你的語句'$ to ='[email protected]''$ subject''$ message''$ headers';'會產生解析錯誤,可能是這樣的'$ to = array('one @ gmail。 com',$ subject,$ message,$ headers);',您可能因此必須更改其他3個語句。 – Kumar 2011-04-18 03:47:01

1

您不需要每個case語句中的{}大括號。如果你從一個窗體中選擇的城市,也許像下面這樣將有助於:

<?php 
$selectedCity = $_GET['city']; 
switch(trim($selectedCity)) 
{ 
    case 'Chennai': 
     $to = "<contents>"; 
     break; 
    case 'Bengaluru': 
     $to = "<contents>"; 
     break; 
    default: // do whatever you want when they've selected something unexpected. 

     break; 
} 
?> 
0
$selectcity = array_map('trim',array("Bengaluru","Coimbatore","Chennai")); 

// the assumiing your value for city is coming form a form and you want to determine 
// address to send to  
switch($_POST['city'];) 
{ 
     case "Bengaluru": 
      $to = "'[email protected]''$subject''$message''$headers'"; 
      break; 

     case "Coimbatore": 
      $to = "'[email protected]''$subject''$message''$headers'"; 
      break; 

     case "Chennai": 
      $to = "'[email protected]''$subject''$message''$headers'"; 
      break; 

}