如果这是一个非常简单的问题,我很抱歉,但我仍在学习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 :(得分:1)
我不确定这是否真的是您的代码中的错误,或者只是在复制和粘贴此帖子时,但请尝试关闭您的引号。
echo("Fill in the blah field");
echo ("Fill in the blah blah field");
答案 1 :(得分: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.
}