如果这是一个非常简单的问题,我很抱歉,但我仍然在学习PHP的诀窍。
我正在用PHP编写一个邮件脚本,它接收表单的内容并将其发送到两个电子邮件中的一个。我让它工作得很好,但我为之创建它的人回来时做了一些编辑,现在我正在努力。
实际上,有两组单选按钮,如果选中"Yes“,还需要填写另一个"additional information”字段。如果勾选“否”,则其他字段可以保留为空。
这就是我到目前为止所知道的:
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(....)
}我知道一定有更好的方法来首先检查一个集合是否有效,然后检查另一个集合是否有效,然后检查所有必填字段是否都已填写...目前,当我提交表单时,我得到的只是一个空白屏幕。
谢谢你的帮忙!
发布于 2010-09-09 00:28:07
您的描述和代码似乎与我无关,但我对名为'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.
}发布于 2010-09-09 00:09:39
我不确定这实际上是你的代码中的错误,还是只是在复制和粘贴这篇文章中的错误,但尝试关闭你的引号。
echo("Fill in the blah field");
echo ("Fill in the blah blah field");https://stackoverflow.com/questions/3669708
复制相似问题