开发者

jquery and ajax in codeigniter

开发者 https://www.devze.com 2023-03-16 09:11 出处:网络
I just want to display a content from database when click a button in codeigniter, here is my script function check()

I just want to display a content from database when click a button in codeigniter,

here is my script

function check()
{

    var base_url="<?php echo base_url();?>"; 
    $.ajax({
    url :  base_url+"/seller/getSubCategory", success : function(data)
    {$('#content').html(data);}});
}

in controller.php

public function getSubCategory()
    {
        $data['result']=$this->propertydetails->getPropertyCategories();
        $this->load->view('sell开发者_开发技巧er/postdetails',$data);
    }

and here is model.php

<?php
class propertydetails extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }   
    function getPropertyCategories()
    {
        $query=$this->db->get('orx_categories');
        if($query->result()>0)
        {
            return $query->result();
        }
        else 
        {
            return $query->result();
        }   
    }
}

?>

when i run this code nothing is happening, it doesn't show any errors, and it returns nothing. help me to solve this?


I've found Charles Proxy invaluable when debugging ajax requests: http://www.charlesproxy.com/ I would also double check that url helper is loaded.


Here is a generic example of what you can try, this assumes that you have a form and you want to use that post data, alternatively you could do something like (".button").click(...

Once the data has been pulled back from the query you can load a view into a variable rather then sending it directly to be output by setting the third parameter of the view loader to true. by doing this you can send multiple views back to the page and it avoids forcing you to write html in the controller.

Javascript code

<!-- language: lang-js -->
$(document).ready(function(){
    $("#form").submit(function(e){
        e.preventDefault();
        $.post("base_url+"/controller/method", $("#form").serialize(),
        function(data){
        if(data.status == "valid"){
            $(".view1").html(data.view1);
            $(".view2").html(data.view2);
        }
        }, "json");
    });
});

And your php controller

class Controller extends CI_Controller {
public function method()
   {
      $this->load->model('Some_model','', TRUE);
      $data['results'] = $this->Some_model->some_function();
      if($data['results']['status'] == "valid"){
         $json['status'] = "valid";
         $json['view1'] = $this->load->view('view1',$data,true);
         $json['view2'] = $this->load->view('view2',$data,true);
         echo json_encode($json);
      }
      else {
         $json['status'] = "invalid";
         $json['error'] = "<h3>Login Failed</h3>";
         echo json_encode($json);
      }
   }
}

and your php model

class Some_model extends CI_Model {
public function some_function()
   {
      $query=$this->db->get('orx_categories');
      if($query->num_rows() > 0) {
         $sata['status'] = "valid";
         foreach($sql->result() as $row) {
            $data['results'][] = $row;
         }   
      }
      else {
         $data['status'] = "invalid";
      }
      return $data;
   }
}


In the controller function when loading a view pass a third boolean parameter true. which means ok get that view and store it in a variable not render it then echo that variable value. see below code of the controller function

public function getSubCategory()
{
    $data['result']=$this->propertydetails->getPropertyCategories();

    //note the third parameter to the view method
    //it means get the content of the seller/postdetails view and store it
    //in a local variable $output
    $output = $this->load->view('seller/postdetails',$data,true);
   //now echo the $output so that the javascript function can consume
   echo $output;
}


Change your script to

function check() {

     var base_url="<?php echo base_url();?>"; 
     $.ajax({
          url : "<?php echo base_url();?>/seller/getSubCategory", 
          success : function(data){
                  $('#content').html(data);
          }
     });
 }

change your controller to :

public function getSubCategory()
    {
        $data = $this->propertydetails->getPropertyCategories();
        echo $data;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消