首页 > 解决方案 > 单击php中的提交按钮后如何使用id获取此名称

问题描述

现在,我想在提交此表单以将这一系列问题上传到数据库时输入带有 id 的无线电此名称。我应该在下面的代码中使用什么:

<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>

标签: phphtmlmysqlform-submit

解决方案


我通常做的是这样的

在表单发布后的 PHP 中

foreach($_POST as $key => $val) {
 if (strpos($key, 'radio_') !== false) {
    $question_id = str_replace('radio_','',$key);
    // now you have the question id that this radio value applies to
 }
}

推荐阅读