下拉填充表ajax codeigniter

时间:2017-02-27 17:40:08

标签: javascript php jquery ajax codeigniter

我想在codeigniter中的ajax中创建一个应用程序,选择一种产品,并出现一个包含产品价格标记的表

所以这就是我所做的,感谢您帮助我使用此代码。 控制器:



public function produit()
	{
		$data['listType']=$this->test_m->findtype();
		return $this->load->view('produit',$data);//List of product types 
	}
  
	public function getprod($idv)
	{
		$this->test_m->idv=$idv;
		$prod=$this->test_m->req_prod();
		header('Content-Type: application/x-json;      charset=utf-8');//to display the table 
      	echo json_encode($prod);
	}




型号:



function findtype()
    {
    	$query=$this->db->get('valve'); 
    	return $query->result(); //dropdown types
    }

    function req_prod()
    {
        if(!is_null($this->idv)){
            $this->db->select('taille,reference,marque,prix,quantite ');
            $this->db->where('idv', $this->idv);
            $prod = $this->db->get('produit');

            // if there are suboptinos for this option...
            if($prod->num_rows() > 0){
                $prod_arr;

                // Format for passing into jQuery loop

                foreach ($prod->result() as $option) {
                    $prod_arr[] = $option->taille;
                    $prod_arr[] = $option->reference;
                    $prod_arr[] = $option->marque;
                    $prod_arr[] = $option->prix;
                    $prod_arr[] = $option->quantite;

                }

                return $prod_arr;
            }
        }

        return;
    }




并查看:



<div id="ess">
	<select name="vl1" id="vl1">
		<option>--select valve--</option>
		<?php foreach($listType as $pr){?>
		<option value="<?php echo $pr->idv;?>"><?php echo $pr->type?></option>
		<?php }?>
	</select><br>
</div>
<p>Nos produits</p>
<div id="pr1">
	<table>
            <tbody>
                <tr id="prod">
                    <td><label>Produits au choix</label></td>
                         
                 </tr>
            </tbody>
         </table>
</div>

<script type="text/javascript">
        $(document).ready(function(){
        $('#vl1').change(function(){ //any select change on the dropdown with id options trigger this code
            var idvlv = $('#vl1').val();  // here we are taking option id of the selected one.

            $.ajax({
                type: "POST",
                url: "/test_c/getprod/"+idvlv , //here we are calling our dropdown controller and getprod method passing the option
 
                success: function(prod) //we're calling the response json array 'suboptions'
                {
                    $.each(prod,function(taille,reference,marque,prix,quantite) //here we're doing a foeach loop round each sub option with id as the key and value as the value
                    {
                        var opt = $('<td/>'); // here we're creating a new select option for each suboption
                        opt.val(taille);
                        opt.val(reference);
                        opt.val(marque);
                        opt.val(prix);
                        opt.val(quantite);
                        $('#prod').append(opt); //here we will append these new select options to a dropdown with the id 'suboptions'
                    });
                }
 
            });
 
        });
    });
    </script>
&#13;
&#13;
&#13;

错误是加载资源失败:服务器响应状态为404(未找到) 并且:[违规]长时间运行的JavaScript任务需要304毫秒

1 个答案:

答案 0 :(得分:0)

在ajax添加之前。

var BASE_URL = "<?php echo base_url(); ?>";

在路线添加中,

$ ['test'] ='控制器/功能';

并在ajax请求中

url: BASE_URL + 'test';
data:{data:idvlv},

并在控制器中,

$这 - &GT;输入 - &GT;柱( '数据');