views:

265

answers:

1

Hello,

I have a method in codeigniter that looks like this,

public function category() {
  //$this->output->enable_profiler(TRUE);
  //echo "Hello function called by ajax";
  $table = $this->uri->segment(3);
  $content_id = $this->uri->segment(4);
  $data['content'] = $this->site_model->get_content($table, $content_id);
  $this->load->view("call", $data);
}

This is method is called via ajax and the results are returned without the user ever leaving the page, this means that URI never gets passed so CI cannot get the segments however I do pull the url out with jquery is there way I can add my url variable that is created in javascript and somehowever get he values I need into PHP?

My javascript

$("a.navlink").click(function (ev) {
  $(this).toggleClass("active");
  ev.preventDefault();
  var id = $(this).attr("id")
  if ($(this).hasClass("active")) {
    $("."+id).remove();
  }
  else {    
    //  $(this).toggleClass("active");
    var url = $(this).attr("href");
    $.ajax ({
      url: "index.php/home/category",
      type: "GET",
      success : function (html) {
        //alert("Success");
        $("#accordion").append($("<div class="+ id +"/>").append(html));
      }
    });
  }
});
A: 

You should add the id to the end of the url in your Javascript.

like so: ...

$.ajax ({
      url: "index.php/home/category/" + id, 
      type: "GET",

...

I think you can also change your PHP to:

<?php
       function category($content_id) {
          // $content_id = $this->uri->segment(4);
          $data['content'] = $this->site_model->get_content($table, $content_id);
          $this->load->view("call", $data);
        }

?>
Darkerstar