简体   繁体   中英

Codeigniter URI and jquery/javascript

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));
      }
    });
  }
});

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);
        }

?>

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