簡體   English   中英

codeigniter 中的 jquery 和 ajax

[英]jquery and ajax in codeigniter

我只想在單擊 codeigniter 中的按鈕時顯示數據庫中的內容,

這是我的腳本

function check()
{

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

在 controller.php

public function getSubCategory()
    {
        $data['result']=$this->propertydetails->getPropertyCategories();
        $this->load->view('seller/postdetails',$data);
    }

這是 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();
        }   
    }
}

?>

當我運行此代碼時,什么都沒有發生,它沒有顯示任何錯誤,也沒有返回任何內容。 幫我解決這個問題?

將您的腳本更改為

function check() {

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

將您的 controller 更改為:

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

在調試 ajax 請求時,我發現 Charles Proxy 非常寶貴: http://www.charlesproxy.com/我還會仔細檢查 Z572D4E421E5E6B9BC11D815E8A027112 幫助是否已加載

這是您可以嘗試的通用示例,假設您有一個表單並且您想要使用該帖子數據,或者您可以執行類似 (".button").click(...

從查詢中提取數據后,您可以將視圖加載到變量中,而不是通過將視圖加載器的第三個參數設置為 true 將其直接發送到 output。 通過這樣做,您可以將多個視圖發送回頁面,並避免強迫您在 controller 中寫入 html。

Javascript代碼

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

還有你的 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);
      }
   }
}

和你的 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;
   }
}

在 controller function 中,加載視圖時傳遞第三個 boolean 參數 true。 這意味着可以獲取該視圖並將其存儲在一個變量中而不是渲染它然后回顯該變量值。 請參閱下面的 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM