PHP 中的两个条件“if”陈述

发布于 2024-11-07 19:52:52 字数 2183 浏览 3 评论 0原文

这可能很简单,但我是 PHP 新手,这不是我的强项,所以请耐心等待!我被困住了!

我已经设置了一个表单,其中包含“是”或“否”答案作为单选按钮。如果用户单击“是”,则会显示另外两个单选按钮选项:ab。如果他们单击no,则会出现单选按钮cd

顺便说一句,我有一些 JavaScript 隐藏了 ab DIV 以及 cd DIV,直到单击yesno,只是为了保持整洁。当 yes< 时,包含 cdno DIV 消失,并替换为 yes DIV no 之后点击 /code>,反之亦然。这一切都工作正常。

我的问题是 PHP 的问题。这不太可能,但如果用户点击no,做出选择,例如d,然后决定将答案更改为yes,然后选择a,脚本仍然返回d。我猜这是因为 d 仍然被选中并且出现在 HTML 流的最后。

我希望我的脚本做的是忽略 cd,即使它们已被选择,如果选择了 yes。同样,如果选择了 no,我希望忽略 ab

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽梦忆笙歌 2024-11-14 19:52:52

使用 == 测试相等性:

if ($yes_or_no == "yes" && $a_or_b != "") $result="$a_or_b";
if ($yes_or_no == "no" && $c_or_d != "") $result="$c_or_d";

我不确定这是否能完全解决您的问题,但也许它会让您开始。

哦,您可以执行 $result = $a_or_b 而不是 $result="$a_or_b",但这不会影响这里的任何内容。

Use == for testing equality:

if ($yes_or_no == "yes" && $a_or_b != "") $result="$a_or_b";
if ($yes_or_no == "no" && $c_or_d != "") $result="$c_or_d";

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.

寄居人 2024-11-14 19:52:52

您当前正在将变量 $yes_or_no 设置为 if 中的值。使用 == 而不是 =。一是赋值,二是比较。

You're currently setting the variable $yes_or_no to the value in the if. Use == instead of =. One is assignment, the other is comparison.

不弃不离 2024-11-14 19:52:52
if ($yes_or_no="yes" && $a_or_b!="") $result="$a_or_b";

问题出在 $yes_or_no = "yes" 上。这是赋值运算符,而不是比较运算符。它说“将 $yes_or_no 设置为 yes,然后返回 true”(这不完全是这样,但很接近)。您需要一个运算符,表示“如果 $yes_or_no 等于 yes,则返回 true”。

= 替换为 == 并且您的代码应该可以工作。

(您的代码在这里被破坏还有另一个原因 - =优先级高于&&,而==的优先级更高,但这不是最大的问题。)

if ($yes_or_no="yes" && $a_or_b!="") $result="$a_or_b";

The problem is with $yes_or_no = "yes". That is the assignment operator, not the comparison operator. It says "set $yes_or_no to yes, then return true" (this isn't exactly it, but it's close). You want an operator that says "return true if $yes_or_no is equal to yes".

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.)

看春风乍起 2024-11-14 19:52:52

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文