PHP Codeigniter: fetch data in dropdown and popula

2019-06-10 01:39发布

问题:

What I'm trying to say is: when you click dropdown or select, it will fetch the data of prod_temno, prod_desc,prod_selluom then populate to the textbox named: idesc and inventoryuom. I hope you can help me. Thanks in advance.

 <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>

This is my 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(); 
    }

I hope this is clear for you guys. Thanks.

回答1:

is this what you want?

<?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:

You can also refer this link -> chaneg event

I am assuming that you have to call the function when someone change the select box. You have to call the model through ajax and then put reponse into textbox:-

$('#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);
     }

  });



});

Your controller should be like this say controller name is 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

}


回答3:

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.


标签: codeigniter