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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化