【发布时间】:2025-12-29 21:05:10
【问题描述】:
我在使用带有 PHP 数据的隐藏表单时遇到了困难。我终其一生都无法弄清楚自己做错了什么。
我的代码应该
- 检查攻击是否成功;
- 如果成功,则从生命值中减去伤害;
- 重写 $health 变量。
- 在下一轮使用新的 $health 值。
问题是,它一直在重置健康值。
这是我的代码(设置为使攻击总是成功):
<?php
$health = $_REQUEST["health"];
$attack = rand(10,20);
$defend = rand(1,9);
$damage = rand(1,5);
$health =50;
if ($attack>$defend){
print "<p>Jim hit the robot for $damage.</p>";
$health = $health - $damage;
print "<p>The robot has $health health remaining.</p>";
} else {
print "<p>Jim missed.</p>";
print "<p>The robot has $health health remaining.</p>";
} // end if statement
print <<<HERE
<input type="text"
name="openMonsterHealth"
value="$health">
<input type="hidden"
name="hdnMonsterHealth"
value="$health">
<input type="submit"
value="click to continue">
HERE;
?>
【问题讨论】:
-
你能否澄清一下这个问题——这与隐藏字段有什么关系?
-
另外,请在所有代码行前放置 4 个空格...我真的需要编辑能力:p
-
:) 已处理。这闻起来像家庭作业,是吗?家庭作业问题是允许的,我只是好奇。
-
@Paolo,什么样的学校教PHP并使用隐藏字段存储数据?我建议任何人立即放弃该课程。他可能是从书本上自学的。
-
@Frank:嗯,我的一个班级的作业允许我们使用我们选择的编程语言,解释 PHP,他本可以自己做隐藏输入法,因此那是他需要指导的地方。 :)