2010-09-08 128 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