PHP Codeigniter:在下拉列表中获取数据并填充到文本框中

时间:2013-06-18 03:42:22

标签: codeigniter

我想说的是:当您点击下拉列表或选择时,它将获取prod_temno,prod_desc,prod_selluom的数据,然后填充到名为:idesc和inventoryuom的文本框中。我希望你能帮助我。提前谢谢。

 <table>
    <tr><form name="form1">
        <td>Item No:</td>
        <td>
        <select type="text" id="inventoryitemno" name="inventoryitemno" >
            <?php
                foreach($itemno as $row){
                    echo "<option id=".$row->prod_id.">".$row->prod_itemno."</option>";
                }
            ?>               
        </select>                                
        </td>
    </tr>
    <tr>
        <td>Description:</td>
        <td><input type="text" id="idesc" name="idesc"/></td>  
    </tr>
     <tr>
        <td>UOM:</td>
        <td><input type="text" id="inventoryuom" name="inventoryuom" /></td>
    </tr>

</table>

这是我的products_model

    public function view_product_itemno(){
        $this->load->database();
        $data = $this->db->query('select prod_id, prod_itemno, prod_desc, prod_inventoryuom from product;');
        return $data->result(); 
    }

我希望你们这很清楚。感谢。

3 个答案:

答案 0 :(得分:0)

您也可以参考此链接 - &gt; chaneg event

我假设当有人更改选择框时你必须调用该函数。你必须通过ajax调用模型,然后将响应放入文本框: -

$('#inventoryitemno').change(function() 
{   

   // write ajax code and call your model and dump the result into your textbox

    $.ajax({
     type:"POST",
     data:"value="+$(this).val(),
     url:"your_controller_path",
     success:function(msg)
     {
        $('#idesc').val(msg);
     }

  });



});

您的控制器应该像这样说控制器名称是product_controller: -

function product_details()
{
   $value = $this->input->post('value'); // value from ajax

   $this->products_model->products_model($value);

   echo "response"; //return the result back to ajax success function

}

答案 1 :(得分:0)

这就是你想要的吗?

<?php
$itemno = array('0'=>'a','1'=>'b','2'=>'c');

?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js" ></script>
<table>
<tr><form name="form1">
    <td>Item No:</td>
    <td>
    <select type="text" id="inventoryitemno" name="inventoryitemno"  onchange="javascript:set_to(this.value);" >
         <?php
             foreach($itemno as $key=>$val){
                echo "<option id=".$key.">".$val."</option>";
            }
        ?>               
    </select>                                
    </td>
</tr>
<tr>
    <td>Description:</td>
    <td><input type="text" id="idesc" name="idesc"/></td>  
</tr>
 <tr>
    <td>UOM:</td>
    <td><input type="text" id="inventoryuom" name="inventoryuom" /></td>
</tr>

</table>
<script type="text/javascript">
function set_to(id)
{

    $('#idesc').val(id);
}
</script>

答案 2 :(得分:0)

What about "<DATALIST>"?

<input type=text name='somename' list='somelist'>
<datalist id="somelist">
<option value=1>One</option>
.
.
<option value=99>Ninety-Nine</option>
</datalist>

You can even fill the list from a DB with mysql
<datalist = id=somelist>
<?
$sql="select * from my_table";
$rs=mysql_query($sql,$mysql_connection);
$row=mysql_fetch_assoc($rs);
do {
 printf("<option value='%s'>",$row['some_field'])}
while($row=mysql_fetch_assoc($rs));

Hope that helps.