【问题标题】:How to get all answers of questions even if not answer in php即使没有在php中回答,如何获得所有问题的答案
【发布时间】:2021-09-13 19:11:10
【问题描述】:

如果用户未回答问题,我需要获得所有问题的答案。为这个问题的结果在数据库上设置 null。

<form action="forms/handle-questions.php" method="post">
<?php foreach ($questions $key => $question) : ?>
  <div class="row">
    <?= $question['question'] ?>
   </div>
   <div class="form-check ">
     <input class="form-check-input" type="radio" name="radio_<?= $question['id'] ?>" 
            value="good">
     <label class="form-check-label"> good</label>
   </div>
   <div class=" form-check ">
     <input class="form-check-input" type="radio" name="radio_<?= $question['id'] ?>" 
            value="medium">
     <label class="form-check-label" >medium</label>
   </div>
   <div class="form-check ">
     <input class="form-check-input" type="radio" name="radio_<?= $question['id'] ?>"
            value="weak">
     <label class="form-check-label">weak</label>
   </div>
   <?php endforeach ?>
   <button type="submit" name="handle_questions">Done</button>
</form>

对于每个问题,我已经得到了带有 id 的答案的问题,但我需要其他答案并将其设置为 null。

 foreach ($_POST as $key => $record) {
    if (strpos($key, 'radio_') !== false) {
        $question_id = str_replace('radio_', '', $key);
        // here get the question id and insert in database 
    }
  }

我真的不知道输出会是这样的

Array
 (
   [radio_1] => good
   [radio_2] => medium
   [radio_3] => medium
   [radio_4] => medium
   [radio_5] => null
   [radio_6] => medium 
   [radio_7] => null
   [radio_8] => null
 )

【问题讨论】:

  • 请提供所需的o/p
  • 我编辑了这样做可以帮助您理解我吗?
  • 试一试答案,让我们知道。

标签: php html mysql forms form-submit


【解决方案1】:

您可以通过添加带有问题 ID 的隐藏字段并过滤表单结果来实现此目的,
像这样:
表格

 <form action="forms/handle-questions.php" method="post">
        <?php foreach ($questions as $key => $question) : ?>
            <div class="row">
                <?= $question['question'] ?>
                <input class="form-check-input" type="hidden" name="question_<?= $question['id'] ?>" value="radio_<?= $question['id'] ?>">
            </div>
            <div class="form-check ">
                <input class="form-check-input" type="radio" name="radio_<?= $question['id'] ?>"
                       value="good">
                <label class="form-check-label"> good</label>
            </div>
            <div class=" form-check ">
                <input class="form-check-input" type="radio" name="radio_<?= $question['id'] ?>"
                       value="medium">
                <label class="form-check-label" >medium</label>
            </div>
            <div class="form-check ">
                <input class="form-check-input" type="radio" name="radio_<?= $question['id'] ?>"
                       value="weak">
                <label class="form-check-label">weak</label>
            </div>
        <?php endforeach ?>
        <button type="submit" name="handle_questions">Done</button>
    </form>

控制器

$answerArray = [];
        $formResult = $_POST;
        foreach ($formResult as $key => $record) {
            if (strpos($key, 'question_') !== false) {
                if(isset($formResult[$record])){
                    $answerArray[$record]=$formResult[$record];
                }else{
                    $answerArray[$record]=null;
                }
                // here get the question id and insert in database
            }
        }
$this->dd($_POST,$answerArray);

输出这样

【讨论】:

    猜你喜欢
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2015-07-19
    • 2022-10-17
    • 1970-01-01
    相关资源
    最近更新 更多