PHP 中的两个条件“if”陈述
这可能很简单,但我是 PHP 新手,这不是我的强项,所以请耐心等待!我被困住了!
我已经设置了一个表单,其中包含“是”或“否”答案作为单选按钮。如果用户单击“是”,则会显示另外两个单选按钮选项:a
和 b
。如果他们单击no
,则会出现单选按钮c
和d
。
顺便说一句,我有一些 JavaScript 隐藏了 a
和 b
DIV 以及 c
和 d
DIV,直到单击yes
或no
,只是为了保持整洁。当 yes< 时,包含
c
和 d
的 no
DIV 消失,并替换为 yes
DIV no
之后点击 /code>,反之亦然。这一切都工作正常。
我的问题是 PHP 的问题。这不太可能,但如果用户点击no
,做出选择,例如d
,然后决定将答案更改为yes
,然后选择a
,脚本仍然返回d
。我猜这是因为 d
仍然被选中并且出现在 HTML 流的最后。
我希望我的脚本做的是忽略 c
和 d
,即使它们已被选择,如果选择了 yes
。同样,如果选择了 no
,我希望忽略 a
和 b
。
HTML 看起来像这样:
<label for="yes_or_no">Yes or No?</label>
<input type="radio" name="yes_or_no" id="yes" value="yes" />
<label for="yes">Yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" />
<label for="no">No</label>
<div id="yes">
<label for="a_or_b" id="a_or_b">A or B?</label>
<input type="radio" name="a_or_b" id="a" value="a" />
<label for="a">A</label>
<input type="radio" name="a_or_b" id="b" value="b" />
<label for="b">B</label>
</div>
<div id="no">
<label for="c_or_d" id="c_or_d">C or D?</label>
<input type="radio" name="c_or_d" id="c" value="c" />
<label for="c">C</label>
<input type="radio" name="c_or_d" id="d" value="d" />
<label for="d">D</label>
</div>
我的 PHP 看起来像这样,但它不能正常工作:
$yes_or_no = $_REQUEST["yes_or_no"] ;
$a_or_b = $_REQUEST["a_or_b"] ;
$c_or_d = $_REQUEST["c_or_d"] ;
if ($yes_or_no="yes" && $a_or_b!="") $result="$a_or_b";
if ($yes_or_no="no" && $c_or_d!="") $result="$c_or_d";
$feedback = "$result";
然后变量 $feedback
通过电子邮件发送给我。
我希望这是有道理的!感谢您的帮助!
马丁.
This is probably as simple as it gets, but I'm new to PHP and it's not my forte, so please bear with me! I'm stuck!
I've got a form set up with a yes or no answer as a radio button. If the user clicks yes
they are presented with two further radio button choices: a
and b
. If they had clicked no
they would have been presented with radio buttons c
and d
.
Incidentally, I've got some JavaScript hiding the a
and b
DIV and the c
and d
DIV until either yes
or no
is clicked, just to keep things tidy. The no
DIV, containing c
and d
disappears and is replaced with the yes
DIV when yes
is clicked after no
, and vice versa. That's all working fine.
My issue is with the PHP. It's not likely, but if the user hits no
, makes a choice, say d
, then decides to change their answer to yes
, then selects a
, the script still returns d
. I guess this is because d
is still selected and appears last in the HTML flow.
What I would like my script to do is ignore c
and d
, even if they've been selected, if yes
is selected. Similarly I'd like a
and b
to be ignored if no
is selected.
A HTML looks something like this:
<label for="yes_or_no">Yes or No?</label>
<input type="radio" name="yes_or_no" id="yes" value="yes" />
<label for="yes">Yes</label>
<input type="radio" name="yes_or_no" id="no" value="no" />
<label for="no">No</label>
<div id="yes">
<label for="a_or_b" id="a_or_b">A or B?</label>
<input type="radio" name="a_or_b" id="a" value="a" />
<label for="a">A</label>
<input type="radio" name="a_or_b" id="b" value="b" />
<label for="b">B</label>
</div>
<div id="no">
<label for="c_or_d" id="c_or_d">C or D?</label>
<input type="radio" name="c_or_d" id="c" value="c" />
<label for="c">C</label>
<input type="radio" name="c_or_d" id="d" value="d" />
<label for="d">D</label>
</div>
My PHP looks like this but it's not working properly:
$yes_or_no = $_REQUEST["yes_or_no"] ;
$a_or_b = $_REQUEST["a_or_b"] ;
$c_or_d = $_REQUEST["c_or_d"] ;
if ($yes_or_no="yes" && $a_or_b!="") $result="$a_or_b";
if ($yes_or_no="no" && $c_or_d!="") $result="$c_or_d";
$feedback = "$result";
The variable $feedback
is then emailed to me.
I hope that makes sense! Thanks for your help!
Martin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
==
测试相等性:我不确定这是否能完全解决您的问题,但也许它会让您开始。
哦,您可以执行
$result = $a_or_b
而不是$result="$a_or_b"
,但这不会影响这里的任何内容。Use
==
for testing equality:I'm not sure if that will completely solve your problem, but perhaps it will get you started.
Oh, and you can do
$result = $a_or_b
instead of$result="$a_or_b"
, but that won't affect anything here.您当前正在将变量
$yes_or_no
设置为if
中的值。使用==
而不是=
。一是赋值,二是比较。You're currently setting the variable
$yes_or_no
to the value in theif
. Use==
instead of=
. One is assignment, the other is comparison.问题出在
$yes_or_no = "yes"
上。这是赋值运算符,而不是比较运算符。它说“将$yes_or_no
设置为yes
,然后返回true
”(这不完全是这样,但很接近)。您需要一个运算符,表示“如果$yes_or_no
等于yes
,则返回true
”。将
=
替换为==
并且您的代码应该可以工作。(您的代码在这里被破坏还有另一个原因 -
=
的 优先级高于&&
,而==
的优先级更高,但这不是最大的问题。)The problem is with
$yes_or_no = "yes"
. That is the assignment operator, not the comparison operator. It says "set$yes_or_no
toyes
, then returntrue
" (this isn't exactly it, but it's close). You want an operator that says "returntrue
if$yes_or_no
is equal toyes
".Replace
=
with==
and your code should work.(There is another reason your code is broken here --
=
has lower precedence than&&
, whereas==
has higher precedence, but that isn't the biggest issue.)您可以使用 js 清除 a 和 b 的任何选择,如果选择的是“否”,则当用户选择“是”时也是如此。
另外,您的 php 中还有其他人提到过的一些错误。
You can use js to clear any selection of a and b is no is selected and the same thing for when user selects yes.
Also you have some bugs in your php that others have mentioned.