PHP - 使用表单输入中的 id 创建一个 switch 语句

2022-01-19 00:00:00 forms switch-statement php

我正在用 PHP 做一个简短的测验,根据 4 个是/否问题告诉你你的想法是什么生物.我已经做到了,根据您对每个问题的回答,您会被带到不同的问题,我主要使用 switch 语句来做到这一点.

I'm making a short quiz in PHP that tells you what creature your thinking of based on 4 yes/no questions. I have made it so that depending on your answer to each question you get taken to a different question, I did this using mainly switch statements.

我的问题是,我有什么方法可以使用表单中的提交按钮中的 id 作为条件的 switch 语句?

My question is is there any way that I can make a switch statement with the condition as the id from the submit buttons from the form?

      echo "<form method ='post' action='Creatures.php'>
     <input type='submit' name='answer' id='1' value='Yes' />
     <input type='submit' name='answer' id='2' value='No' />
     </form>";

这是我用来显示按钮的表单,我已经做到了,以便每个按钮都有不同的 id,所以在测验结束时,根据最后按下的按钮的 id 是什么,我可以显示正确答案.以下是我尝试执行的操作,但没有成功.

This is the form I'm using to display the buttons, I have made it so that each button has a different id so at the end of the quiz, depending on what the id of the last button pressed is, I can display the right answer. Below is what I tried to do but it didn't work.

switch($_POST['id'])
{
 case 1: echo "It's a goldfish";
 case 2: echo "It's a eel";
}

此外,这些字段是唯一在整个网页中使用 id 的字段,关于如何使其正常工作的任何建议,不一定使用 switch 语句?

Also these fields are the only ones which use id's throughout the whole web page, any suggestions on how I can get this to work properly, not necessarily using switch statements?

推荐答案

switch($_POST['answer']) {

case 'Yes':
   // do soemthing
   break;
case 'No':
   // do something
   break;
}

相关文章