2

我对 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 的变量,我不知道为什么。

4

3 回答 3

2

对于 rgroups 3,4 和 5,您的值不是 rgroup1 和 rgroup2 的预期格式。您已将您的值作为与您的 switch 案例不匹配的字符串,如优秀、好等。

于 2013-02-19T12:30:53.433 回答
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;
于 2013-02-19T12:34:16.320 回答
0

命名相同的开关案例的所有默认变量。因此,如果未选择任何选项 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;`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;

?>
于 2013-02-19T12:36:19.113 回答