Javascript 如何用php scritp填充html表单,哪一个获取数组?

Javascript 如何用php scritp填充html表单,哪一个获取数组?,javascript,php,html,Javascript,Php,Html,我有一个html页面: <form action="" method="post"> <div class="row"> <div class="col-12"> <h2 class="tm-question-header">Question 1</h2> <p style="color: black" id="q1"&g

我有一个html页面:

   <form action="" method="post">
        <div class="row">
            <div class="col-12">
                <h2 class="tm-question-header">Question 1</h2>
                <p style="color: black" id="q1">Who is the best?</p>
                <div class="tm-q-choice-container">
                    <label class="tm-q-choice">
                            <input class="tm-radio-group-1 with-gap" name="q1" type="radio" value="q1_a1" />
                            <span id="q1a1" style="color: black">Ivo</span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                            <input class="tm-radio-group-1 with-gap" name="q1" type="radio" value="q1_a2" />
                            <span id="q1a2" style="color: black">Pavel</span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                            <input class="tm-radio-group-1 with-gap" name="q1" type="radio" value="q1_a3" />
                            <span id="q1a3" style="color: black">Dominik</span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                            <input class="tm-radio-group-1 with-gap" name="q1" type="radio" value="q1_a4" />
                            <span id="q1a4" style="color: black">Vendula</span>
                    </label>
                </div>
                <hr>
                <br>
            </div>
            </div>
            <div class="col-12 text-center tm-submit-container">
                <button type="submit" href="#" class="btn btn-primary tm-btn-submit">Submit</button>
            </div>
    </form>

我怎么能做到?一些提示或解决方案?我需要准备所有表格项目吗?如果是,我如何删除空问题?非常感谢。

您只需要一个带计数器的循环

<div class="col-12">
    <?php
        $count = 1;
        foreach($questions as $question) {
            $q = 'q' . $count; //q1, q2, q3 etc
            ?>
                <h2 class="tm-question-header">Question <?= $count ?></h2>
                <p style="color: black" id="<?= $q ?>"><?= $question[$q] ?></p>
                <div class="tm-q-choice-container">
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a1" />
                        <span id="<?= $q ?>a1" style="color: black"><?= $question[$q.'a1'] ?></span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a2" />
                        <span id="<?= $q ?>a2" style="color: black"><?= $question[$q.'a2'] ?></span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a3" />
                        <span id="<?= $q ?>a3" style="color: black"><?= $question[$q.'a3'] ?></span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a4" />
                        <span id="<?= $q ?>a4" style="color: black"><?= $question[$q.'a4'] ?></span>
                    </label>
                </div>
                <hr>
                <br>
            <?
            $count++;
        }
    ?>
</div>

问题:





在每个循环中,我创建一个名为
$q
的变量,该变量包含类似
q1
q2
(取决于
$count
变量,该变量在每个循环中从1开始,在每个循环中递增1)等内容,我在多个位置使用该变量从数组中提取每个问题的数据

这将用数组中的每个问题填充整个表单

<div class="col-12">
    <?php
        $count = 1;
        foreach($questions as $question) {
            $q = 'q' . $count; //q1, q2, q3 etc
            ?>
                <h2 class="tm-question-header">Question <?= $count ?></h2>
                <p style="color: black" id="<?= $q ?>"><?= $question[$q] ?></p>
                <div class="tm-q-choice-container">
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a1" />
                        <span id="<?= $q ?>a1" style="color: black"><?= $question[$q.'a1'] ?></span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a2" />
                        <span id="<?= $q ?>a2" style="color: black"><?= $question[$q.'a2'] ?></span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a3" />
                        <span id="<?= $q ?>a3" style="color: black"><?= $question[$q.'a3'] ?></span>
                    </label>
                    <br>
                    <label class="tm-q-choice">
                        <input class="tm-radio-group-1 with-gap" name="<?= $q ?>" type="radio" value="<?= $q ?>_a4" />
                        <span id="<?= $q ?>a4" style="color: black"><?= $question[$q.'a4'] ?></span>
                    </label>
                </div>
                <hr>
                <br>
            <?
            $count++;
        }
    ?>
</div>