简体   繁体   English

使用ajax和codeigniter从数据库中检索信息并将其返回

[英]retrieving information out of database with ajax and codeigniter and returning it

I have a site showing products and when you click a product you get the product details. 我有一个显示产品的网站,当您单击产品时,您会获得产品详细信息。 Now every product has a few different models that are all stored in the database. 现在,每个产品都有几种不同的模型,所有模型都存储在数据库中。 So when you select an option in the selectbox all product details have to change to the according model. 因此,当您在选择框中选择一个选项时,所有产品详细信息都必须更改为相应的型号。 Problem is when i return my data its get messed up because it loads the view again. 问题是当我返回我的数据时,它会变得混乱,因为它再次加载了视图。 I just want the records changing dynamic on what you select. 我只希望您选择的内容动态变化。

productdetail_view productdetail_view

<?php foreach($productsdetail as $row) { ?>
    <div class="container">
      <ol class="breadcrumb">
  <?php echo set_breadcrumb(); ?>
</ol>
 <div class="row">
 <div class="border clearfix">
  <div class="productContainer">
    <div class="productImg">
        <img class='voorbeeldimg' src="<?php echo base_url();?>uploads/<?php echo $row->foto;  ?>" alt="">
    </div>
    </div>
    <div class="test" id="test" style="height:200px; background-color:grey;"></div>
    <div class="productInfo">
    <h2>Nordia <?php echo $row->bestelnr; ?></h2>
    <h4><?php echo $row->naam; ?></h4>
    <p><?php echo $row->beschrijving; ?></p><br/>
        <label for="modellen">Modellen</label>
    <select  name="modellen" id="selection" class="form-control">
        <option value="600X400">600X400</option>
        <option value="700X500">700X500</option>
    </select><br/>
    <p><b>Draagvermogen:</b> <?php echo $row->draagvermogen; ?>kg</p>
    <p><b>Laadvlak:</b> <?php echo $row->laadvlak; ?>mm</p>
    <p><b>Wiel:</b> <?php echo $row->wiel; ?>mm</p>
    <p><b>Eigen gewicht:</b> <?php echo $row->gewicht; ?>kg</p>
    <p><b>Bestel nr: </b><?php echo $row->bestelnr; ?></p><br/>
    <label for="aantal">Aantal</label>
    <input type="number" class="form-control" id="aantal" placeholder="Stuks">
    <h3><?php echo $row->prijs; ?> €</h3>
    <h6>Excl 21% btw</h6>

<br/>
        <p><a href="" class="btn btn-default btn-lg"><span class="glyphicon glyphicon-plus-sign"></span>&nbsp;Winkelwagentje</a></p>
  </div>
  </div>
  </div>
      <br/>
</div> <!-- /container -->
  <?php } ?>

Controller 控制者

function productdetail(){
        $data = array();
        $id = $this->uri->segment(5, 0);
        $data['productInfo'] = array();
        $this->load->model('Site_model');
        $title['title'] = 'Nordia - Steekkar';
        $data['productsdetail'] = $this->Site_model->get_productdetail($id);
        $this->load->view('head_view', $title);
        $this->load->view('nav_view');
        $this->load->view('productdetail_view',$data);
        $this->load->view('footer_view');
    }

    function modelopties(){
        $data = array();
        $where = array();
        $where['modellen'] = $this->input->post('modellen');
        $this->load->model('Site_model');
        $data['option'] = $this->Site_model->get_productoptions($where);
        $this->load->view('productdetail_view',$data);
    }

Model 模型

function get_productoptions($where) {
 return $this->db->where('laadvlak')->get('tblproducts')->row();

} }

ajax 阿贾克斯

<script>
    $(function(){
        $('#selection').change(function(){
             var modellen = $(this).val();
             $.ajax({
                url : 'site/modelopties',
                type : "POST",
                data : modellen,
                success : function(data){
                    $('#test').html(data);

                },
             }); 
        });
    }) 
    </script>

Without knowing what the errors behind "its get messed up" are, it makes it somewhat difficult to know exactly what the issue is...however, from an initial glance, your javascript function is incorrect. 不知道“搞砸了”背后的错误是什么,这使得很难确切地知道问题是什么...但是,乍看之下,您的javascript函数是不正确的。

Taken from http://api.jquery.com/jQuery.ajax/ 取自http://api.jquery.com/jQuery.ajax/

The data option can contain either a query string of the form key1=value1&key2=value2 , or an object of the form {key1: 'value1', key2: 'value2'} . data选项可以包含格式为key1 = value1&key2 = value2的查询字符串,也可以包含格式为{key1:'value1',key2:'value2'}的对象

At the moment you are sending just the value of modellen to the site/modelopties controller/function and as a result $this->input->post('modellen') is looking in the post array key called 'modellen' (which it obviously cannot find). 目前,您modellen的值发送到site/modelopties控制器/函数,结果$this->input->post('modellen')在名为'modellen'的发布数组键中查找显然找不到)。

I would recommend changing your javascript function to the below: 我建议将您的javascript函数更改为以下内容:

$('#selection').change(function(){
    var modellen = $(this).val();
    $.ajax({
        url : 'site/modelopties',
        type : "POST",
        data : { modellen: modellen }, // or modellen=modellen
        success : function(data){
            $('#test').html(data);

        },
    }); 
    return false; // Prevents page from reloading
});

Hope that helps... 希望有帮助...

EDIT 编辑

Firstly, update the javascript function with return false; 首先,使用return false更新javascript函数 as this will prevent the page from reloading when you change the value of the select box. 因为这样可以防止在更改选择框的值时重新加载页面。

Secondly, the current javascript function will return whatever is being output from the modelopties controller function (which in this case is the productdetail_view ). 其次,当前的javascript函数将返回从modelopties控制器函数输出的任何内容(在本例中为productdetail_view )。 In order to just return the values of the database (not the whole view) you will need to modify your modelopties function. 为了只返回数据库的值(而不是整个视图),您将需要修改modelopties函数。 I would suggest creating a new view that displays just the part of the view you want to load into the #test div. 我建议创建一个新视图,该视图仅显示要加载到#test div中的视图的一部分。

Controller 控制者

<?php
function modelopties() {
    $data = array();
    $where = array();
    $where['modellen'] = $this->input->post('modellen');
    $this->load->model('Site_model');
    $data['option'] = $this->Site_model->get_productoptions($where);
    $this->load->view('productdetail_partial_view', $data); // new view file
}
?>

View (productdetail_view_partial) 查看 (productdetail_view_partial)

<?php
foreach ($option as $row) {
    echo $row . '<br />'; // manipulate the data however you want it to be displayed
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM