简体   繁体   English

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

[英]PHP - making a switch statement using id from form input

I'm making a short quiz in PHP that tells you what creature your thinking of based on 4 yes/no questions. 我正在用PHP进行一个简短的测验,根据4个是/否问题告诉你你的想法是什么。 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. 我已经做到这一点,根据你对每个问题的答案你会得到一个不同的问题,我主要使用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? 我的问题是,有什么方法可以使用条件作为表单中提交按钮的id来创建一个switch语句?

      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>";

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. 这是我用来显示按钮的形式,我已经做了这样,每个按钮都有不同的ID,所以在测验结束时,根据按下的最后一个按钮的id,我可以显示右边的回答。 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";
}

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? 这些字段也是唯一在整个网页中使用id的字段,有关如何使其正常工作的建议,不一定使用switch语句?

switch($_POST['answer']) {

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

There are many errors to this. 这有很多错误。 Please study the following: 请学习以下内容:

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

switch($_POST['answer'])
{
   case 'Yes':
        echo "It's a goldfish";
        break;
   case 'No':
        echo "It's a eel";
        break;
}

Also see this and this . 也看到这个这个

You need 你需要

break;

after each statement and your form input is wrong ( answer instead of id ). 在每个语句之后,您的表单输入错误( answer而不是id )。 The NAME attribute is what determines the key in the POST array (the ID is for client side css and javascript only). NAME属性决定POST数组中的键(ID仅用于客户端css和javascript)。

And as tigrang has pointed out, the value is what is being posted, not the ID, so the array would look like: 正如tigrang指出的那样,值是发布的内容,而不是ID,所以数组看起来像:

$_POST['answer']='Yes' $ _ POST [ '答案'] = '是'

So your statement would be 所以你的陈述是

switch($_POST['answer'])
{
 case 'Yes': echo "It's a goldfish";break;
 case 'No': echo "It's a eel";break;
}

This won't work. 这不行。 when the browser sends the data from your form to the server in the form of key-value pairs. 当浏览器以键值对的形式将表单中的数据发送到服务器时。 The keys are your form names, and the values are the values of the inputs. 键是表单名称,值是输入的值。

so to get the value of your input name answer, you use $_GET['answer']; 所以要获得输入名称答案的值,请使用$_GET['answer'];

which will either give you "Yes" or "No", because those are the values. 这将是“是”或“否”,因为这些是值。 You should also do some error checking since people can send arbitrary data to your servers, for example someone could visit yoursite.com/creatures.php?answer=somerandomstuffthatidontexpect 您还应该进行一些错误检查,因为人们可以向您的服务器发送任意数据,例如有人可以访问yoursite.com/creatures.php?answer=somerandomstuffthatidontexpect

Unless you do it with jQuery/js, you will struggle. 除非你使用jQuery / js,否则你会很困难。 Various browsers will interpret this differently (own experience), so just go jQuery way :) 各种浏览器会以不同的方式解释(自己的体验),所以只需要jQuery方式:)

http://www.vancelucas.com/blog/ie6-and-multiple-button-submit-elements/ - this should help http://www.vancelucas.com/blog/ie6-and-multiple-button-submit-elements/ - 这应该有帮助

<form method ='post' name="formAnswer1" action=''>
    <input type='submit' name='answer' value='Yes' />
</form>

<form method ='post' name="formAnswer2" action=''>
    <input type='submit' name='answer' value='No' />
</form>

<?php
switch($_POST['answer'])
{
   case 'Yes': echo "Yes";
   break;

   case 'No': echo "No";
   break;
}
?>

How about this? 这个怎么样?

switch(key($_POST['answer']))
{
case 1: 
    echo "answer 1"; break;
case 2: 
    echo "answer 2"; break;
}
echo "<form method ='post' action='/tmp/test.php'>
<input type='submit' name='answer[1]' value='Yes' />
<input type='submit' name='answer[2]' value='No' />
</form>";

I think it works the way you want it to. 我认为它按照你想要的方式工作。 But your approach is a bit counter-intuitive from programmers POV; 但是你的方法与程序员POV有点反直觉; consider using radiobuttons + submit button. 考虑使用radiobuttons +提交按钮。

I was trying to use $_GET with a switch statement for my own class revision. 我试图将$ _GET与switch语句一起用于我自己的类修订版。 This is what worked for me. 这对我有用。 The idea was to display something based on the grades entered on the form. 我们的想法是根据表单上输入的等级显示内容。

 <form action="processGrade.php" method "get">
 Enter your grade: <input type = "text" name ="grade">
 <input type = "submit">
 </form>

//On my processGrade.php form: //在我的processGrade.php表单上:

<?php
echo "You have entered: ".$mygrade = $_GET['grade'].". ";

switch($mygrade){

case 100:
    echo " Your grade is A";
    break;

case 70:
    echo " Your grade is B+";
    break;

default: echo "Ungraded";
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM