PHP-使用表单输入中的ID制作switch语句 [英] PHP - making a switch statement using id from form input
问题描述
我正在用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屋!