【问题标题】:PHP can't POST the selected value from a dropdownPHP 无法从下拉列表中发布选定的值
【发布时间】:2016-09-07 09:28:30
【问题描述】:
          <form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
        <?php
        echo "<div class = 'dropdown_container'><select class = 'dropdown select xs-mt1' name = 'line' id = 'line'>";
        foreach($ptv_json_bus as $item)
        {
          $stopid = $item['stop_id'];
          $stopname = $item['location_name'];

          ?>

          <option value = "<?php echo $stopid;?>"> <?php echo $stopname;?></option>

          <?php
        }//end foreach
        ?>
        <input type = "hidden" value ="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
        <input type = "hidden" value ="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
      </select>

      <button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
    </div>


  </form>

大家好。以上是我正在使用的代码。

我想要实现的是将所选值的停止 ID 和停止名称(从下拉列表)发布到另一个文件,然后将数据添加到 mySQL。

我现在的问题是我当前的代码实际上只是发布了 $stopid 和 $stopname 的最后一个设置值,这不是我需要的。

我已经坚持了很长一段时间,我会喜欢它,这里有人可以为我指出正确的方向!

谢谢

【问题讨论】:

    标签: php json forms post


    【解决方案1】:

    &lt;input&gt; 元素移到&lt;select&gt; 元素之外。 您只能将 &lt;option&gt; 元素放在您的 &lt;select&gt; 元素内。

    我已经稍微改进了你的版本。

    <form action="addbusstop.php" method="POST" id="produceJSON" enctype="multipart/form-data">
        <div class = 'dropdown_container'>
            <select class="dropdown select xs-mt1" name="line" id="line">
                <?php
                foreach ($ptv_json_bus as $item) {
                    $stopid = $item['stop_id'];
                    $stopname = $item['location_name'];
                    echo "<option value='$stopid'>$stopname</option>";
                }
                ?>
            </select>
            <input type="hidden" value="<?php echo $stopid; ?>" name="stop_id" id="stop_id">
            <input type="hidden" value="<?php echo $stopname; ?>" name="stop_name" id="stop_name">
    
            <button type="submit" class="button button-latrobe-orange xs-ml2" onclick="changeState3()" id="submitstop2" name="submitstop2">OK</button>
        </div>
    </form>
    

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多