【问题标题】:How to show data from database depending on user's choice from a select如何根据用户从选择中的选择显示数据库中的数据
【发布时间】:2015-06-10 08:48:50
【问题描述】:

我必须进行选择,当用户从中选择选项时,它应该根据此选择显示来自数据库的数据。我不能使用提交。仅应显示此选择和选择选项时,以显示此选择的数据。我是 jQuery 新手,我做不到。我的看法是:

 <select name="myselect" id="myselect" >

        <option value=''" . set_select('size, '') . " > </option>

        <option value="1" <?php echo set_select('size', '1'); ?> role="menuitem">1</option>
        <option value="2" <?php echo set_select('size', '2'); ?> role="menuitem">2</option>
        <option value="3" <?php echo set_select('size', '3'); ?> role="menuitem">3</option>
        <option value="4" <?php echo set_select('size', '4'); ?> role="menuitem">4</option>
 </select>

我的模型是:

<?php

    public function average_price_by_week() {

            $this->db->select(*);
            $this->db->from('items');

            $this->db->where('size', $size); //this $size should be value from select that user has selected

            $query=$this->db->get();
            return $query->result_array();

        }



?>

【问题讨论】:

标签: php jquery database select


【解决方案1】:

试试这个,它可能对你有帮助。

我用过JQuery Ajax。在使用下面的代码之前还包括JQuery library

$('#myselect').change(function(){
    var size = $('#myselect').val();
    if(size != ''){
        $.ajax({
            type:'post',
            url:"/index.php/average_incomes_by_customer/" + size,
            cache:false,
            success: function(returndata){
                console.log(returndata); //"returndata" is the result got from the DB. Use it where you want.
            }
        });
    }
});

控制器:

public function average_incomes_by_customer($size) { 
    $data['average_price']=$this->receivedOrders_model->average_price_by_week($size);
    $this->load->view('charts', $data); 
}

型号:

public function average_price_by_week($size) {
    $this->db->select(*);
    $this->db->from('items');
    $this->db->where('size', $size); //this $size should be value from select that user has selected           
    $query=$this->db->get();
    return $query->result_array();
}

谢谢。

【讨论】:

  • 如何更改从数据库返回的数据 - 我应该使用 Get ot post 吗?我的意思是在我的模型中:$this->db->where('size', $size);如何从ajax传递这个值?谢谢。我的控制器是: public function average_incomes_by_customer() { $data['average_price']=$this->receivedOrders_model->average_price_by_week(); $this->load->view('charts', $data); }
  • 你在使用任何框架吗?您可以使用POSTGET
  • 更新了答案。请检查一下。
  • 我不详细了解如何在URL中调用控制器动作。检查谷歌关于在ajaxcodeignitor上提供网址
  • 使用type: 'GET' 而不是type: 'POST'
猜你喜欢
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
相关资源
最近更新 更多