我在尝试使下面的语句按照我想要的方式工作时遇到了麻烦。
如果未填写至少一个文本字段,我正在尝试显示订单表单的错误消息。下面是我的PHP代码片段。 'cookieGiftBox'是用户可以选择的复选框的名称,如果选中它,它们必须在提供的文本字段中以所需的口味输入一定量的cookie。如果选中复选框但是没有填写文本字段,如何显示要显示的错误消息?
<?php
if (isset($_POST['cookieGiftBox'])
&& (!isset($_POST['sugar'])
|| ($_POST['chocolateChip'])
|| ($_POST['chocolateChipPecan'])
|| ($_POST['whiteChocolateRaspberry'])
|| ($_POST['peanutChocolateChip'])
|| ($_POST['peanutButter'])
|| ($_POST['tripleChocolateChip'])
|| ($_POST['whiteChocolateChip'])
|| ($_POST['oatmealRaisin'])
|| ($_POST['cinnamonSpice'])
|| ($_POST['candyChocolateChip'])
|| ($_POST['butterscotchToffee'])
|| ($_POST['snickerdoodle']))) {
$error.="<br />Please enter an Amount for Cookie Flavors";
}
?>
答案 0 :(得分:1)
&安培;&安培;优先于||,因此您必须使用括号来获得预期结果:
if checkbox is checked and not (sugar is checked or chocolatechip is checked)
实际上,要检查是否选择无,您可以执行以下操作:
if checkbox is checked and sugar is not entered and chocolatechip is not entered...
或同等的:
if (isset($_POST['cookieGiftBox']) &&
!(
isset($_POST['sugar']) ||
isset($_POST['chocolatechip'])
)
) { error ...}
。
如果您想了解更多信息,请搜索有关Boolean algebra。
更新:在你的例子和正确的语法中,我作为一个例子的句子是这样的(对于第一句,注意不是(!)和字段周围的括号,而不是复选框):
if (isset($_POST['cookieGiftBox']) &&
!isset($_POST['sugar']) &&
!isset($_POST['chocolatechip'])
) { error...}
或者第二句,可能更容易理解(注意&amp;&amp;而不是||):
(true && !false && !false)
如果选中giftbox复选框时,如果没有设置sugar,chocolatechips等字段,则使用ands确保它只是true(因此显示错误)。
因此,如果选中该复选框,并且未设置任何字段,则它如下所示:
(true && true && true)
相当于true
(true && !true && !false)
,因此显示错误。
如果选中该复选框并输入糖,则它将如下所示:
(true && false && true)
,等。到false
,等。到 [{"latitude":"40.651602","longitude":"-111.918734","title":"64439","content":"test"}][{"latitude":"41.330500","longitude":"-111.851600","title":"80283","content":"test"}][{"latitude":"40.624500","longitude":"-111.976500","title":"95964","content":"test"}][{"latitude":"40.715020","longitude":"-111.494717","title":"96995","content":"test"}][{"latitude":"40.664200","longitude":"-111.843295","title":"101724","content":"test"}][{"latitude":"40.513917","longitude":"-111.403612","title":"245697","content":"test"}][{"latitude":"40.509000","longitude":"-111.419919","title":"245716","content":"test"}][{"latitude":"40.473700","longitude":"-111.414499","title":"245742","content":"test"}][{"latitude":"40.509943","longitude":"-111.402999","title":"245792","content":"test"}][{"latitude":"40.512200","longitude":"-111.477263","title":"245874","content":"test"}][{"latitude":"40.526623","longitude":"-111.475199","title":"245904","content":"test"}][{"latitude":"40.530400","longitude":"-111.487500","title":"245905","content":"test"}][{"latitude":"40.526619","longitude":"-111.473531","title":"245921","content":"test"}][{"latitude":"40.517013","longitude":"-111.487827","title":"245926","content":"test"}][{"latitude":"40.519500","longitude":"-111.497780","title":"245940","content":"test"}][{"latitude":"40.511981","longitude":"-111.409799","title":"245944","content":"test"}][{"latitude":"39.373348","longitude":"-111.574996","title":"245946","content":"test"}][{"latitude":"40.514900","longitude":"-111.402590","title":"245982","content":"test"}][{"latitude":"40.513700","longitude":"-111.467712","title":"246093","content":"test"}][{"latitude":"40.515600","longitude":"-111.398192","title":"246124","content":"test"}]
,因此没有显示错误。
如果同时输入糖和巧克力碎片,也不会显示错误。
答案 1 :(得分:0)
我有类似问题:
我尝试使用:
if (isset($this->context->controller->php_self) && ($this->context->controller->php_self != 'category') || ($this->context->controller->php_self != 'product') || ($this->context->controller->php_self != 'index')){ ... }
这不起作用,我改为:
if (isset($this->context->controller->php_self) && ($this->context->controller->php_self != 'category') && ($this->context->controller->php_self != 'product') && ($this->context->controller->php_self != 'index')){ ... }
我不知道这是对的吗?
编辑。这是不正确的,但现在我不知道如何解决它。