0

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');      
    }
3
  • you did everything correct, but what is your problem?? "pass data value to controller using ajax" means what?? Commented Jun 15, 2017 at 11:49
  • 1
    TYPO: url:"<?=base_url()?>.index.php/Home/product_grid", typo remove dot before index.php in ajax url Commented Jun 15, 2017 at 11:49
  • 1
    Does the ajax call reach your controller's action? because the URL "<?=base_url()?>.index.php/Home/product_grid" seems weird to me Commented Jun 15, 2017 at 11:49

2 Answers 2

3

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);
   }
});
Sign up to request clarification or add additional context in comments.

9 Comments

Hello Rohan This is also not perform properly
Check in your browser's console for any errors and make sure that the AJAX request is made properly.
I get data properly but not send to controller
i Chcked and eroor alert can be run and display the html code. and not alert success function
Give me your working Code URL, otherwise you have to check your controller's code. I think you are getting some errors in PHP code. Enable Logs in config file and check it for any errors.
|
0
<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 .

2 Comments

Concatenation operator is used to add multiple strings in JS not in a single string.
Hey there. The + in the string will indeed not do what you think it will, you would need to close the string, put the plus, and reopen ("<?=base_url()?>"+"index.php/Home/product_grid"). However it's really not needed in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.