PHP - making a switch statement using id from form input

一个人想着一个人 提交于 2019-12-31 02:35:06

问题


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.

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

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

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?


回答1:


switch($_POST['answer']) {

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



回答2:


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.




回答3:


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'];

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




回答4:


Unless you do it with jQuery/js, you will struggle. Various browsers will interpret this differently (own experience), so just go jQuery way :)

http://www.vancelucas.com/blog/ie6-and-multiple-button-submit-elements/ - this should help




回答5:


You need

break;

after each statement and your form input is wrong (answer instead of id). The NAME attribute is what determines the key in the POST array (the ID is for client side css and javascript only).

And as tigrang has pointed out, the value is what is being posted, not the ID, so the array would look like:

$_POST['answer']='Yes'

So your statement would be

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



回答6:


<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;
}
?>



回答7:


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; consider using radiobuttons + submit button.




回答8:


I was trying to use $_GET with a switch statement for my own class revision. 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:

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


来源:https://stackoverflow.com/questions/10758853/php-making-a-switch-statement-using-id-from-form-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!