PHP - 使用表单输入中的 id 创建一个 switch 语句 [英] PHP - making a switch statement using id from form input

查看:27
本文介绍了PHP - 使用表单输入中的 id 创建一个 switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 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;
}

这篇关于PHP - 使用表单输入中的 id 创建一个 switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆