【问题标题】:Why I do not get radio button value with php?为什么我没有使用 php 获得单选按钮值?
【发布时间】:2020-03-10 08:12:38
【问题描述】:

我在带有表单的页面中传递一个单选按钮的值,我想在另一个 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') 

什么都不返回

【问题讨论】:

  • 你错过了 $。 echo row[0](开始)

标签: php html forms post radio


【解决方案1】:

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

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

插入method='post':

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-06-18
        • 1970-01-01
        • 2012-06-10
        • 2011-08-30
        • 1970-01-01
        • 2023-03-09
        • 2018-10-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多