【问题标题】:PHP - check if a html name attribute is available (check for undefined index)PHP - 检查 html 名称属性是否可用(检查未定义的索引)
【发布时间】:2018-10-29 17:33:01
【问题描述】:

我正在尝试使用这些类型的输入创建一个测验表单:

<input type="date" name="Q1"/>
<input type="checkbox" id="2a" name="Q2[]"/>
<input type="checkbox" id="2b" name="Q2[]"/>
<input type="checkbox" id="2c" name="Q2[]"/>
<input type="radio" name="Q3a"/>
<input type="radio" name="Q3b"/>
<input type="radio" name="Q3c"/>
<input type="radio" name="Q4a"/>
<input type="radio" name="Q4b"/>

在 php 中,我想获取这些输入并使用 for 循环检查他们的答案:

$answer = array(//a whole bunch of answers);
for ($i=0; $i<6; $i++){
    $response = $_POST["Q".$i];
    $response = sanitise_input($response);
    if (in_array($response, $answer)){
        $point++;
    }
}
echo($point);

这个基本概念是可行的,但是 Q2、3 和 4 呢? Q2 有 [],Q3 和 Q4 在 name 属性中有 a、b 和 c(不在 Q4 中),我不知道如何编码,如果找不到 "Q".$i,请尝试找到"Q".$i."[]""Q".$i."a"等等......

提前致谢

编辑:好的,按照建议,我已将输入名称更改为

<input type="date" name="Q1"/>
<input type="checkbox" id="2a" name="Q2[]"/>
<input type="checkbox" id="2b" name="Q2[]"/>
<input type="checkbox" id="2c" name="Q2[]"/>
<input type="radio" name="Q3[a]"/>
<input type="radio" name="Q3[b]"/>
<input type="radio" name="Q3[c]"/>
<input type="radio" name="Q4[a]"/>
<input type="radio" name="Q4[b]"/>

这意味着我需要一种不同的方式来给这些问题打分,因为这些问题是在一个数组中返回的,我认为 in_array 仍然无法处理这个问题。

【问题讨论】:

  • $aCharCode=97; $varName = "Q".$i.chr($aCharCode+$j) 其中 $j 将从零变为可能的字母数。请参阅 PHP chr 函数、ord 函数和此 ASCII 表 asciitable.com
  • 是的,但我不知道如何检查$_POST["Q".$i]; 何时返回“未定义索引”错误。如果我可以确定我可能会使用那种方法......
  • 您可以添加带有问题 ID 的隐藏输入,因此在 PHP 中您将知道要循环多少输入。也检查if ( isset($_POST[$inputName]) ) { }
  • 为什么不用更多的数组符号来构建你的名字,例如而不是Q3aQ[3][a] ?它使以后循环和获取数据变得更加容易。
  • @TudorIlisoi 谢谢,现在对我有用。

标签: php


【解决方案1】:

我觉得改名字比较好。

<input type="date" name="Q1"/>
<input type="checkbox" id="2a" name="Q2[]"/>
<input type="checkbox" id="2b" name="Q2[]"/>
<input type="checkbox" id="2c" name="Q2[]"/>
<input type="radio" name="Q3[a]"/>
<input type="radio" name="Q3[b]"/>
<input type="radio" name="Q3[c]"/>
<input type="radio" name="Q4[a]"/>
<input type="radio" name="Q4[b]"/>

【讨论】:

    【解决方案2】:

    试试这个:

    $answer = array(/*a whole bunch of answers*/);
    $firstQuestionIndex = 1;
    $maxQuestions = 100000;
    $step = 1;
    for ($i = $firstQuestionIndex; $i < $maxQuestions; $i++) {
        switch($step) {
            case 1: 
                if(isset($_POST["Q".$i]) && !empty($_POST["Q".$i])) {      
                    $response = $_POST["Q".$i];
                    $response = sanitise_input($response);
                    if (in_array($response, $answer)){
                        $point++;
                    }
                } else {
                    $step++;
                    $i = $firstQuestionIndex - 1;
                }
                break;
            case 2: 
                if(isset($_POST["Q".$i.'[]']) && !empty($_POST["Q".$i.'[]'])) {      
                    $response = $_POST["Q".$i.'[]'];
                    $response = sanitise_input($response);
                    if (in_array($response, $answer)){
                        $point++;
                    }
                } else {
                    $step++;
                    $i = $firstQuestionIndex - 1;
                }
                break;
            default:
                for ($j = 0; $j < $maxQuestions; $j++) {
                    if(isset($_POST["Q".$i.chr($j+ord('a'))]) && !empty($_POST["Q".$i.chr($j+ord('a'))])) {      
                        $response = $_POST["Q".$i.chr($j+ord('a'))];
                        $response = sanitise_input($response);
                        if (in_array($response, $answer)){
                            $point++;
                        }
                    } else {
                        if($j == 0) {
                            $i = $maxQuestions;
                        } else {
                            $i = $firstQuestionIndex - 1;
                        }
                        $j = $maxQuestions;
                    }
                }
                break;
        }
    }
    echo($point);
    

    【讨论】:

    • 当您在输入名称中使用数组表示法[] 时,它会以an array 的形式返回给您。换句话说,你不会用[]取回任何密钥。
    【解决方案3】:

    好的,我已经解决了问题的打分问题,这些问题有一个数组作为响应 using this page

    for ($i=1; $i<=4; $i++){
        $response = $_POST["Q".$i];
        if (in_array($response, $answer)){
            $point++;
        } elseif (count(array_diff($_POST["Q".$i], $answer)) == 0){
            $point++;
        }
    }   
    

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2015-06-04
      • 2014-01-10
      • 2015-07-31
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多