2010-09-08 123 views
0

如果這是一個非常簡單的問題,我很抱歉,但我仍然在學習PHP的知識。PHP如果語句

我正在PHP中編寫一個郵件腳本,它接受一個表單的內容並將其發送給兩個電子郵件中的一個。我的工作很完美,但是我爲之創作的人回來後做了一些編輯,現在我正在掙扎。

實質上,有兩組單選按鈕,如果選中「是」,則還需要填寫另一個「附加信息」字段。如果選擇「否」,則其他字段可以保持空白。

這是我到目前爲止有:

if ($var1 == "String" AND $var2 =="") 
{ 
    echo("Fill in the blah field"); 
} 
elseif ($var3 == "Yes" AND $var4 == "") 
{ 
    echo ("Fill in the blah blah field"); 
} 
elseif ($var1 !="" AND $var2 !="" AND $var7 !="") 
{ 
    mail(....) 
    echo(....) 
} 

我知道必須有先檢查一個更好的方式,如果一組驗證,然後如果對方確實,然後如果所有必填字段填充....目前,當我提交表單,我所得到的只是一個空白的屏幕。

感謝您的幫助!

+0

你能發表正在使用的實際表單嗎? – webbiedave 2010-09-08 16:08:53

+0

你嘗試過'error_reporting(-1); ini_set('display_errors','On');'在腳本的頂部查看所有錯誤? http://php.net/error_reporting – janmoesen 2010-09-08 16:12:49

回答

1

您的描述和代碼似乎與我無關,但我對名爲'var'和'blah'字段的變量感到困惑。但是,根據你的描述,也許這會幫助你。

$set_2_required = !empty($_GET['radio_set_1'] && $_GET['radio_set_1'] == 'yes'; 

if ($set_2_required && empty($_GET['radio_set_2'])){ 
    echo 'ERROR: You must fill out radio set 2.'; 
} else { 
    // Send your mail. 
} 

編輯:我想我以前的評論有你所需要的所有邏輯片段,但也許這將更接近你實際寫的東西。

// With these ternary operators, you logic further down can rely on a NULL 
// value for anything that's not set or an empty string. 
$dropdown_1 = !empty($_GET['dropdown_1']) ? $_GET['dropdown_1'] : NULL; 
$dropdown_2 = !empty($_GET['dropdown_2']) ? $_GET['dropdown_2'] : NULL; 
$field_1 = !empty($_GET['field_1']) ? $_GET['field_1'] : NULL; 
$field_2 = !empty($_GET['field_2']) ? $_GET['field_2'] : NULL; 

// This 'valid' variable lets you avoid nesting and also return multiple errors 
// in the request. 
$valid = TRUE; 
if (!$field_1 && $dropdown_1 == '<string makes field required>'){ 
    echo 'ERROR: Field 1 is required for this dropdown selection.'; 
    $valid = FALSE; 
} 
if (!$field_2 && $dropdown_2 == '<string makes field required>'){ 
    echo 'ERROR: Field 2 is required for this dropdown selection.'; 
    $valid = FALSE; 
} 

// A final check if the logic gets complicated or the form on the front end 
// wants to check one thing to determine pass/fail. 
if (!$valid){ 
    echo 'ERROR: One or the other fields is required.'; 
} else { 
    // Everything's fine, send the mail. 
} 
+0

對不起,現在我回頭看它很混亂。 Var1是下拉菜單中的一個值。在所有這些值中,如果選擇了其中的一個,則var2包含某些內容。如果選擇了下拉列表中的任何其他選項,則無關緊要。所以「字符串」是指下拉選項的值。 'blahs'指的是需要填寫的特定領域。其中有兩個。 – Vecta 2010-09-08 16:32:17

1

我不確定這是否是您的代碼中的錯誤,或者僅僅是複製和粘貼此帖子,但請嘗試關閉引號。

echo("Fill in the blah field"); 
echo ("Fill in the blah blah field"); 
+0

糟糕,我認爲這只是一個換位錯誤。謝謝。 – Vecta 2010-09-08 16:18:51