首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP If语句

问PHP If语句
EN

Stack Overflow用户
提问于 2010-09-09 00:06:38
回答 2查看 140关注 0票数 0

如果这是一个非常简单的问题,我很抱歉,但我仍然在学习PHP的诀窍。

我正在用PHP编写一个邮件脚本,它接收表单的内容并将其发送到两个电子邮件中的一个。我让它工作得很好,但我为之创建它的人回来时做了一些编辑,现在我正在努力。

实际上,有两组单选按钮,如果选中"Yes“,还需要填写另一个"additional information”字段。如果勾选“否”,则其他字段可以保留为空。

这就是我到目前为止所知道的:

代码语言:javascript
复制
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(....)
}

我知道一定有更好的方法来首先检查一个集合是否有效,然后检查另一个集合是否有效,然后检查所有必填字段是否都已填写...目前,当我提交表单时,我得到的只是一个空白屏幕。

谢谢你的帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-09-09 00:28:07

您的描述和代码似乎与我无关,但我对名为'var‘的变量和'blah’字段感到困惑。但是,根据你的描述,也许这会对你有帮助。

代码语言:javascript
复制
$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.
}

编辑:我认为我之前的评论包含了你所需要的所有逻辑片段,但也许这篇评论会更接近你实际写的东西。

代码语言:javascript
复制
// 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.
}
票数 1
EN

Stack Overflow用户

发布于 2010-09-09 00:09:39

我不确定这实际上是你的代码中的错误,还是只是在复制和粘贴这篇文章中的错误,但尝试关闭你的引号。

代码语言:javascript
复制
echo("Fill in the blah field");
echo ("Fill in the blah blah field");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3669708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档