如何从下拉列表选择中检索数据到表格? [英] How to retrieve data from drop down list selection to a table?

查看:48
本文介绍了如何从下拉列表选择中检索数据到表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力学习如何编程,这是我的第一个项目.我知道我应该阅读教程,但我有点坚持.真的很想自己完成它,但它越来越令人沮丧..有人至少可以指导我朝着正确的方向前进吗?那么,如何根据下拉列表选择将数据回显到表格单元格?非常感谢任何帮助.到目前为止的代码:http://jsfiddle.net/uFEPn/3/

I'm trying to learn how to program and this is my first project. I know i should read the tutorials but im kinda stuck with it. really wanting to accomplish it by myself, but it's getting frustrating.. can somebody at least guide me in the right direction? So, how do i echo the data to the table cells based on the drop down list selection? Any help much appreciated. code so far: http://jsfiddle.net/uFEPn/3/

<select>
    <option>-- Select lot --</option>
    <?php
    mysql_connect('localhost','root','');
    mysql_select_db('pl_base');
    $query="SELECT DISTINCT  lot_number FROM pl_table";
    $result=mysql_query($query);
    while(list($lot_number)=mysql_fetch_row($result)) {
        echo "<option value=\"".$lot_number."\">".$lot_number."</option>";
    }
   ?>
    </select>
</br>

<table border="1" id="table">
  <tr>
    <th width=80 height=30>Lot<br/>number</th>
    <th width=110 height=30>Description</th>
    <th width=90 height=30>Pallet<br/>number</th>
    <th width=60 height=30>Net</th>
    <th width=60 height=30>Gross</th>
  </tr>
  <tr>
    <td>#</td>
    <td rowspan="5">echo results here</td>
    <td><b>Total</b></td>
    <td>#</td>
    <td>#</td>
  </tr>
    <td> # </td>
    <td colspan="3">#</td>
  </tr>

  <tr>
    <th>&</th>
    <th>&</th>
    <th>&</th>
    <th>&</th>
  </tr>

  </table>

数据库中的表格:

+--------------------------+-------------------------+---------+-------+
|    id   |   lot_number   | descr  | pallet_number  | net     | gross |
+--------------------------+-------------------------+---------+-------+
|    1    |       111      |  black |         1      |  800    | 900   |
|    2    |       111      |  black |         2      |  801    | 901   |
|    3    |       111      |  black |         3      |  802    | 902   |
|    4    |       222      |  white |         1      |  800    | 900   |
|    5    |       222      |  white |         2      |  801    | 901   |
|    6    |       222      |  white |         3      |  802    | 902   |
+--------------------------+-------------------------+---------+-------+

推荐答案

在 select 元素中选择批号不会直接使用 php 更新.由于 php 是一种服务器端语言,因此您必须将数据作为表单提交然后显示.

Choosing the lot number in a select element will not update directly using php. since php is a server side language you would have to submit the data as a form and then display it.

    <?php 
     mysql_connect('localhost','root','');
        mysql_select_db('pl_base');
        $query="SELECT DISTINCT  lot_number FROM pl_table";
        $result=mysql_query($query);
     ?>  
     <form action="" method="POST">
     <select name="option_chosen">
        <option>-- Select lot --</option>
        <?php
            while(list($lot_number)=mysql_fetch_row($result)) {
            echo "<option value=\"".$lot_number."\">".$lot_number."</option>";
        }
       ?>
        </select>

     <input type="submit" value="Submit" />
    </form>
    </br>

    <table border="1" id="table">
      <tr>
        <th width=80 height=30>Lot<br/>number</th>
        <th width=110 height=30>Description</th>
        <th width=90 height=30>Pallet<br/>number</th>
        <th width=60 height=30>Net</th>
        <th width=60 height=30>Gross</th>
      </tr>

      <?php 
      if($_SERVER['REQUEST_METHOD'] =='POST')
 { $option_chosen=$_POST['option_chosen'];
     $query="SELECT * FROM pl_table WHERE lot_number='$option_chosen'";
     $run=mysqli_query($query);
     $row=mysqli_fetch_array($run, MYSQLI_ASSOC);

    echo "<tr><td>".$row['lot_number']."</td>";
    echo  "<td>".$row['descr']."</td><td>".$row['pallet_number']."</td>";
    echo "<td>".$row['net']."</td><td>".$['gross']."</td></tr>";

}
?>

         </table>

这篇关于如何从下拉列表选择中检索数据到表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆