简体   繁体   中英

I have to pass data value to controller using ajax

View File product_grid

 <div data_id="<?php echo $ro['category_id']; ?>" name="id" class="cbp-filter-item">
                                <?php echo $ro['category_name']; ?></div>

// try to post data using ajax                  

<script type="text/javascript">
    $(document).ready(function() {
    $('.cbp-filter-item').click(function(){
            var id=$(this).attr('data_id');
        //  alert(data_id);
            jQuery.ajax({
                url:"<?=base_url()?>.index.php/Home/product_grid",
                type:'POST',
                data: {"id":id},
                success:function(data) {
                   alert(data);
                }
            });
        });
    });
</script>

Hear i can pass the id to view but not work properly please help me to solved this problem

Controller ::
public function product_grid()
    {
        $id= $this->input->post('id');      
    }

You must have to use print or echo otherwise there will be no response sent to client side so use echo like,

public function product_grid(){
     $id= $this->input->post('id');
     echo 'Data-Id is: '.$id;
}

And use the URL like,

url:"<?=base_url()?>index.php/Home/product_grid", // let there is / in the end of base_url()

AJAX Code,

jQuery.ajax({
   url:"<?=base_url()?>index.php/Home/product_grid",
   type:'POST',
   data: {id:id}, // no need of quotes to JSON Object keys
   success:function(data) {
       alert(data);
   }
});
<script type="text/javascript">
    $(document).ready(function() {
    $('.cbp-filter-item').click(function(){
            var id=$(this).attr('data_id');
        //  alert(data_id);
            jQuery.ajax({
                url:"<?=base_url()?>+index.php/Home/product_grid",
                type:'POST',
                data: {"id":id},
                success:function(data) {
                   alert(data);
                }
            });
        });
    });
</script>


url:"<?=base_url()?>+index.php/Home/product_grid"

Use + for concatination instead of .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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