php选择选项编程

时间:2015-07-06 14:01:09

标签: php mysql

我怎么能在php中告诉他们是否回答了问题1,2,3,5,6中的问题是答案#34;答案A" 如果他们回答问题1,3,5,10,12,那么问题就是#34;答案B" 在其他情况下,放出答案C"

    <form action="questions".php" method="POST">
<p>1.Question1?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>2.Question2?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>3.Question3?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>4.Question4?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>5.Question5?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<p>6.Question6?</p>
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
7.Question7?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
8.Question8?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
9.Questione9?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
10.Question10?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
11.Question11?
<p><select> <option value="YES">YES</option> <option value="NO">NO</option> </select></p>
<input name="submit" type="submit" value="submit" /> </form>

2 个答案:

答案 0 :(得分:1)

以下是使用选择如何完成此操作的示例。我认为单选按钮在这里会更好用。

<form method="POST" action="questions.php">
            <p>1.Question1?</p>
    <p>
        <select name="questions[1]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>2.Question2?</p>
    <p>
        <select name="questions[2]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>3.Question3?</p>
    <p>
        <select name="questions[3]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>4.Question4?</p>
    <p>
        <select name="questions[4]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>5.Question5?</p>
    <p>
        <select name="questions[5]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <p>6.Question6?</p>
    <p>
        <select name="questions[6]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    7.Question7?
    <p>
        <select name="questions[7]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    8.Question8?
    <p>
        <select name="questions[8]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    9.Questione9?
    <p>
        <select name="questions[9]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    10.Question10?
    <p>
        <select name="questions[10]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    11.Question11?
    <p>
        <select name="questions[11]">
            <option value="YES">YES</option>
            <option value="NO">NO</option>
        </select>
    </p>
    <input name="submit" type="submit" value="submit" />
</form>

然后在PHP方面进行处理......(我也不确定你的条件是如何工作的,这是一种可行的方法。)

foreach($_POST['questions'] as $key => $question) {
        switch($key) {
            case 1:
            case 2:
            case 3:
            case 5:
            case 6:
                if($question == 'YES') {
                    echo 'the answer A';
                } else {
                    echo 'the answer C';
                }
            break;
            case 4:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
                if($question == 'YES') {
                    echo 'the answer B';
                } else {
                    echo 'the answer C';
                }
            break;
        }
    }

输出:

Question #1=YES<br />Question #2=NO<br />Question #3=NO<br />Question #4=NO<br />Question #5=NO<br />Question #6=NO<br />Question #7=NO<br />Question #8=NO<br />Question #9=NO<br />Question #10=NO<br />Question #11=NO<br />

答案 1 :(得分:0)

您必须为选择提供名称,例如:

echo "<select name='questionOne'>";
echo "<option value='YES'>YES</option>";
echo "<option value='NO'>NO</option>";
echo "</select><br/>";
echo "<select name='questionTwo'>";
echo "<option value='YES'>YES</option>";
echo "<option value='NO'>NO</option>";
echo "</select><br/>";

然后做一个条件测试:

if($_POST['questionOne'] == 'YES')
{
//do something
}
elseif($_POST['questionOne'] == 'NO')
{
//do something else
}