在提交表单后显示选择了哪个单选按钮

时间:2013-02-19 12:25:09

标签: php html forms

我是PHP新手,我目前正在进行调查。用户在单选按钮上选择表单中的问题答案。每个问题都有4种可能性,并在每个问题下分组。提交表单后,页面应显示每个组中的哪个按钮被选中。目前的代码是:

<form action="thankyou.php" method="post">
  <p>How easy would you say it is to create new carers?</p>
   <input type="radio" name="rgroup1" value="rg1v1"> Excellent
   <input type="radio" name="rgroup1" value="rg1v2"> Good
   <input type="radio" name="rgroup1" value="rg1v3"> Average
   <input type="radio" name="rgroup1" value="rg1v4"> Poor</br>

  <p>How easy is it to generate a report?</p>
   <input type="radio" name="rgroup2" value="rg2v1"> Excellent
   <input type="radio" name="rgroup2" value="rg2v2"> Good
   <input type="radio" name="rgroup2" value="rg2v3"> Average
   <input type="radio" name="rgroup2" value="rg2v4"> Poor</br>

  <p>How easy is it to add and alter timesheets?</p>
   <input type="radio" name="rgroup3" value="excellent"> Excellent
   <input type="radio" name="rgroup3" value="good"> Good
   <input type="radio" name="rgroup3" value="average"> Average
   <input type="radio" name="rgroup3" value="poor"> Poor</br>

  <p>How good would you rate the Packages Of Care functions? </p>
   <input type="radio" name="rgroup4" value="excellent"> Excellent
   <input type="radio" name="rgroup4" value="good"> Good
   <input type="radio" name="rgroup4" value="average"> Average
   <input type="radio" name="rgroup4" value="poor"> Poor</br>

  <p>Question 5</p>
   <input type="radio" name="rgroup5" value="excellent"> Excellent
   <input type="radio" name="rgroup5" value="good"> Good
   <input type="radio" name="rgroup5" value="average"> Average
   <input type="radio" name="rgroup5" value="poor"> Poor</br>
   </br>
      Please  Enter Your Comments Below:
   </br></br>
        <textarea name="comments" width="100px" rows="4" cols="50" wrap="wrap"></textarea>
   </br></br>
        <input type="submit">
   <br/><br/>
</form>

和我的用于“thankyou.php”页面的PHP是:

<?php 

switch($_POST['rgroup1']) {
    case "rg1v1":
        $value = "Radio Group 1 - Value 1 was selected.";
        break;
    case "rg1v2":
        $value = "Radio Group 1 - Value 2 was selected.";
        break;
    case "rg1v3":
        $value = "Radio Group 1 - Value 3 was selected.";
        break;
    case "rg1v4":
        $value = "Radio Group 1 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 1";
}

switch($_POST['rgroup2']) {
    case "rg2v1":
        $value2 = "Radio Group 2 - Value 1 was selected.";
        break;
    case "rg2v2":
        $value2 = "Radio Group 2 - Value 2 was selected.";
        break;
    case "rg2v3":
        $value2 = "Radio Group 2 - Value 3 was selected.";
        break;
    case "rg2v4":
        $value2 = "Radio Group 2 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 2";
}

switch($_POST['rgroup3']) {
    case "rg3v1":
        $value3 = "Radio Group 3 - Value 1 was selected.";
        break;
    case "rg3v2":
        $value3 = "Radio Group 3 - Value 2 was selected.";
        break;
    case "rg3v3":
        $value3 = "Radio Group 3 - Value 3 was selected.";
        break;
    case "rg3v4":
        $value3 = "Radio Group 3 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 3";
}

switch($_POST['rgroup4']) {
    case "rg4v1":
        $value4 = "Radio Group 4 - Value 1 was selected.";
        break;
    case "rg4v2":
        $value4 = "Radio Group 4 - Value 2 was selected.";
        break;
    case "rg4v3":
        $value4 = "Radio Group 4 - Value 3 was selected.";
        break;
    case "rg4v4":
        $value4 = "Radio Group 4 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 4";
}

switch($_POST['rgroup5']) {
    case "rg5v1":
        $value5 = "Radio Group 5 - Value 1 was selected.";
        break;
    case "rg5v2":
        $value5 = "Radio Group 5 - Value 2 was selected.";
        break;
    case "rg5v3":
        $value5 = "Radio Group 5 - Value 3 was selected.";
        break;
    case "rg5v4":
        $value5 = "Radio Group 5 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 5";
}


echo $value;
echo $value2;
echo $value3;
echo $value4;
echo $value5;

?>

我收到一条错误消息,说我没有为value2,3,4和5定义变量,我不知道为什么。

3 个答案:

答案 0 :(得分:2)

对于rgroups 3,4和5,您的值不是group1和group2的预期格式。你把你的值设置为优秀,良好等字符串,这些字符串与你的开关案例不匹配。

答案 1 :(得分:2)

对于3,4和5,radio部分值非常好,良好,平均,差且不起作用。 'rg3v1','rg3v2','rg3v3'和'rg3v4'。因此,当执行进入以下代码时,

switch($_POST['rgroup3']) {
    case "rg3v1":
        $value3 = "Radio Group 3 - Value 1 was selected.";
        break;
    case "rg3v2":
        $value3 = "Radio Group 3 - Value 2 was selected.";
        break;
    case "rg3v3":
        $value3 = "Radio Group 3 - Value 3 was selected.";
        break;
    case "rg3v4":
        $value3 = "Radio Group 3 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 3";
}

switch($_POST['rgroup4']) {
    case "rg4v1":
        $value4 = "Radio Group 4 - Value 1 was selected.";
        break;
    case "rg4v2":
        $value4 = "Radio Group 4 - Value 2 was selected.";
        break;
    case "rg4v3":
        $value4 = "Radio Group 4 - Value 3 was selected.";
        break;
    case "rg4v4":
        $value4 = "Radio Group 4 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 4";
}

switch($_POST['rgroup5']) {
    case "rg5v1":
        $value5 = "Radio Group 5 - Value 1 was selected.";
        break;
    case "rg5v2":
        $value5 = "Radio Group 5 - Value 2 was selected.";
        break;
    case "rg5v3":
        $value5 = "Radio Group 5 - Value 3 was selected.";
        break;
    case "rg5v4":
        $value5 = "Radio Group 5 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 5";
}

它没有分配任何内容,并在您尝试打印以下变量时显示错误,

echo $value3;
echo $value4;
echo $value5;

答案 2 :(得分:0)

switch case的所有默认变量名为same。因此,如果没有选择选项,则使用值变量。相应地更改其他默认值会起作用。

<?php 

switch($_POST['rgroup1']) {
    case "rg1v1":
        $value = "Radio Group 1 - Value 1 was selected.";
        break;
    case "rg1v2":
        $value = "Radio Group 1 - Value 2 was selected.";
        break;`enter code here`
    case "rg1v3":
        $value = "Radio Group 1 - Value 3 was selected.";
        break;
    case "rg1v4":
        $value = "Radio Group 1 - Value 4 was selected.";
        break; 

        default:
        $value = "No radio has been selected for Radio Group 1";
}

switch($_POST['rgroup2']) {
    case "rg2v1":
        $value2 = "Radio Group 2 - Value 1 was selected.";
        break;
    case "rg2v2":
        $value2 = "Radio Group 2 - Value 2 was selected.";
        break;
    case "rg2v3":
        $value2 = "Radio Group 2 - Value 3 was selected.";
        break;
    case "rg2v4":
        $value2 = "Radio Group 2 - Value 4 was selected.";
        break; 

        default:
        $value2 = "No radio has been selected for Radio Group 2";
}

switch($_POST['rgroup3']) {
    case "rg3v1":
        $value3 = "Radio Group 3 - Value 1 was selected.";
        break;
    case "rg3v2":
        $value3 = "Radio Group 3 - Value 2 was selected.";
        break;
    case "rg3v3":
        $value3 = "Radio Group 3 - Value 3 was selected.";
        break;
    case "rg3v4":
        $value3 = "Radio Group 3 - Value 4 was selected.";
        break; 

        default:
        $value3 = "No radio has been selected for Radio Group 3";
}

switch($_POST['rgroup4']) {
    case "rg4v1":
        $value4 = "Radio Group 4 - Value 1 was selected.";
        break;
    case "rg4v2":
        $value4 = "Radio Group 4 - Value 2 was selected.";
        break;
    case "rg4v3":
        $value4 = "Radio Group 4 - Value 3 was selected.";
        break;
    case "rg4v4":
        $value4 = "Radio Group 4 - Value 4 was selected.";
        break; 

        default:
        $value4 = "No radio has been selected for Radio Group 4";
}

switch($_POST['rgroup5']) {
    case "rg5v1":
        $value5 = "Radio Group 5 - Value 1 was selected.";
        break;
    case "rg5v2":
        $value5 = "Radio Group 5 - Value 2 was selected.";
        break;
    case "rg5v3":
        $value5 = "Radio Group 5 - Value 3 was selected.";
        break;
    case "rg5v4":
        $value5 = "Radio Group 5 - Value 4 was selected.";
        break; 

        default:
        $value5 = "No radio has been selected for Radio Group 5";
}


echo $value;
echo $value2;
echo $value3;
echo $value4;
echo $value5;

?>