douyujun0152 2013-09-23 21:02
浏览 16
已采纳

如何从PHP变量中的HTML选择中保存所选项?

I have an HTML select where the drop down list is created from a SQL query.

I'm wondering how I can then save the item that the user selects into a PHP variable that I can pass on to other PHP pages.

Thanks.

<tr>
  <td>DRM Staff List</td><span class="required">*</span>:<br />
    <td>
      <select name="unit">
          <?php 
              $conn = oci_connect("username", "password", "url");
              $sql = 'select distinct "DRM Primary" from GIS_DATA_LOAD where "DRM Primary" is not null order by "DRM Primary" asc' ;
              $stid = oci_parse($conn, $sql);
              $success = oci_execute($stid);
              echo $success;
              while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC))
              {
                  echo "<option value=\"unit1\">" . $row['DRM Primary'] . "</option>";
              }
          ?>
      </select>
    </td>
</tr>
  • 写回答

4条回答 默认 最新

  • douzhongjiu2263 2013-09-23 21:31
    关注

    Add you code inside the form tag as below and make a form submit action ($_POST or $_GET) using javascipt as onselect. Since the code is on client side so you have to for sure submit it to the server to save the selected option in a php variable.

        <tr>
        <td>DRM Staff List</td><span class="required">*</span>:<br />
        <td>
          <form action="" method="POST" name="myform">
          <select name="unit" onchange="this.form.submit()>
              <?php 
                  $conn = oci_connect("username", "password", "url");
                  $sql = 'select distinct "DRM Primary" from GIS_DATA_LOAD where "DRM Primary" is not null order by "DRM Primary" asc' ;
                  $stid = oci_parse($conn, $sql);
                  $success = oci_execute($stid);
                  echo $success;
                  while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC))
                  {
                      echo "<option value=\"unit1\">" . $row['DRM Primary'] . "</option>";
                  }
              ?>
          </select>
          </form>
        </td>
    </tr>
    

    // now to save the submitted form as value in a php we will use the following code

    <?php
    if(isset($_POST['myform']))
    $selected_unit=$_POST['unit'];
    ?>
    

    you can also use onselect() function ... not sure about that.. Hope it helps..thanks :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line