您的描述和代码似乎与我无关,但我被名为“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.
}