【问题标题】:dynamic radio button options for multiple questions, sending bad array data多个问题的动态单选按钮选项,发送错误的数组数据
【发布时间】:2019-05-06 08:59:10
【问题描述】:

我有一个用于调查的表格。问题在一张表中,可能的回答(pr)在另一张表中。当一个响应被赋予一个 pr 字段时,pr_count 将加一。

问题是,在表单输入行上,我尝试了不同的方法来获取数组。
目前我得到的数组是:

survey => 1
1 => 3
2 => 4
submitted => submitted
confirm => Submit Survey

我只需要1=>32=>4 那将是问题1 答案是pr_id 3 完美!我如何在没有调查、提交和确认的情况下得到这个? `

echo '<form method="post"action="/survey1">';
echo '<input type="hidden" name="survey" value="'.$survey.'">';
while ($i <= $qcount){
    //query gets questions  for a particular survey 
    $stmt = $link->prepare('SELECT question_num, survey_question.question_text as question, survey_question.question_id
    FROM survey_question
    WHERE survey_id = ?
    AND question_num = ?
    ORDER BY survey_question.question_num');
    $stmt->execute(array($survey, $i));
        while ($row = $stmt->fetch()) { 
            echo $row['question'] . "<br>";
    //query gets possible responses
                $stmt2 = $link->prepare('SELECT question_num, pr_num, survey_question.question_id, survey_pr.pr_text as possible, pr_id 
                FROM survey_question, survey_pr 
                WHERE survey_id = ?
                AND question_num = ?
                AND survey_question.question_id = survey_pr.question_id 
                ORDER BY survey_question.question_num, pr_id');
                $stmt2->execute(array($survey, $i));
                    while ($row2 = $stmt2->fetch()) {   
                    $qid = $row2['question_id'];
                    $prid= $row2['pr_id'];

                        echo  '<input type="radio" name="'.$qid.'" value="'. $prid .'">'.$row2['possible'].'<br> ';

            }
            }
        $i++;   
            }

    echo '<input type="hidden" name="submitted" value="submitted"/>';
    echo '<input name="confirm" type="submit" class="button" value="Submit Survey"/>';  

【问题讨论】:

  • echo ''.$row2['possible'].'';
  • 试图更好地解释这一点表单提出了可能回答的问题,而循环显示下一个可能回答的问题。我需要 if 语句的提交值。并且提交是显而易见的。我从来没有不只是通过单选按钮命名的东西。
  • 抱歉,代码示例让您的问题更加模糊。您的查询是否为submittedconfirm 等返回一行?
  • 旁注:你确实有一个结束 &lt;/form&gt; 标签,对吧?

标签: php database forms dynamic radio


【解决方案1】:

你得到的结果数组是你提交的整个表单(所有输入字段)。

因此,要从结果数组中删除 survey =&gt; 1,您需要从示例中删除第 2 行:

echo '<input type="hidden" name="survey" value="'.$survey.'">';

submitted =&gt; submittedconfirm =&gt; Submit Survey 来自示例的最后两行:

echo '<input type="hidden" name="submitted" value="submitted"/>';
echo '<input name="confirm" type="submit" class="button" value="Submit Survey"/>';

为了保留调查中的所有数据而无需任何其他字段,您可以删除未使用的隐藏输入并使用不带name 属性的提交按钮,如下所示:

echo '<form method="post" action="/survey1">';
while ($i <= $qcount) {
    // your loop as is
}
echo '<input type="submit" class="button" value="Submit Survey" />';
echo '</form>';

【讨论】:

  • 谢谢,但是我需要获取调查编号,并且需要知道是否已提交表单以使 if 语句起作用。我不明白为什么我会遇到这样的问题。如果我更改 name='question[]' 那么我只能在两个问题中选择一个单选按钮。
  • 好的,如果我做对了,您只需过滤结果数组以仅保留 question_id =&gt; pr_id,对吗?你可以用array-filter函数来做到这一点:
  • 像这样:$questions = array_filter($post, 'is_int', ARRAY_FILTER_USE_KEY); 其中$post 是您生成的带有 POST 数据的数组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 2021-02-16
  • 2011-08-24
  • 2017-09-07
  • 1970-01-01
相关资源
最近更新 更多