为什么我没有使用 php 获得单选按钮值?

Why I do not get radio button value with php?

我在带有表单的页面中传递单选按钮的值,我想在另一个 php 文件中获取所选选项的值,但我无法获取该值。

第一页:

<form class="itemSelection" action="../php/itemSelection1Save.php"><br><br>
    <h1>Recommended Item(s)</h1>
    <table class="itemTableRecom">
        <thead>
            <tr>
                <th>Selected Item Type</th>
                <th>Recommended Item Type</th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($_SESSION['itemsInfo'] as $row) {
                ?>
                <tr>
                    <td><input type="radio" name="selectedItem" id="selectedItem" required="" value="<?php echo $row[0] ?>"></td>
                    <td><?php
                        echo row[0]</td>
                </tr>
                <?php
            }
            ?>
        </tbody>
    </table><br>
    <input type="submit" value="Next"/>
</form>

第二个文件:

$_SESSION['selectedItem1'] = filter_input(INPUT_POST, 'selectedItem');

filter_input(INPUT_POST, 'selectedItem') 

returns 没什么

您的表单使用方法get(如果未指定,则为默认方法):

<form class="itemSelection" action="../php/itemSelection1Save.php">

插入method='post':

<form class="itemSelection" action="../php/itemSelection1Save.php" method='post'>

在HTML部分做如下修改,添加method="post"

<form class="itemSelection" action="../php/itemSelection1Save.php" method="post">

在 PHP 部分进行以下更改(可选):

同时在filter_input中添加filter_option。 filter 要使用的过滤器的 ID 或名称。默认为 FILTER_DEFAULT,这导致没有过滤。

filter_input(INPUT_POST, 'selectedItem',filter_option) 

您需要添加表单方法,您可以尝试我的代码,我只是为您编写代码,希望它会起作用!

<form class="itemSelection" method="post" action="../php/itemSelection1Save.php">
    <h1>Recommended Item(s)</h1>
    <table class="itemTableRecom">
        <thead>
            <tr>
                <th>Selected Item Type</th>
                <th>Recommended Item Type</th>
            </tr>
        </thead>
        <tbody>
            <?php 

                foreach ($_SESSION['itemsInfo'] as $row) {

                ?>

                <tr>
                    <td><input type="radio" name="selectedItem" value="<?php echo $row[0]; ?>"></td>
                    <td><?php echo $row[0]; ?></td>
                </tr>    

              <?php } ?>   

        </tbody>
    </table>
    <input type="submit" name="submit" value="Next">
</form>