javascript - Codeigniter 3 上的 loadData() 函数

标签 javascript php codeigniter codeigniter-3

如何在 CodeIgniter 3 上使用 JavaScript loadData 函数? 我有一个交易 View ,其中有要购买的详细项目,我想使用 loadData 加载详细 View 。

这是我的文件结构:

myproject:
 -application
   --controller
     ---admin
       -----dashboard.php
       -----transaction.php
     ---home.php
   --model
     ---mymodel.php
   --view
     ---transaction
       ----transaction_index.php <- the view that I want to use for calling
       ----transaction_detail.php <- the view that I want to call
     ---home.php

我的 transaction_index 主要部分:

<div id="t_detail"></div>

脚本

$(document).ready(function(){
function loadData(args){
    $("#t_detail").load("<?php echo base_url('admin/transaction/getDetail'); ?>");
}
loadData();

function emptying(args){
    $("#id").val();
    $("#name").val();
    $("#category").val();
    $("#price").val();
    $("#total").val();
}
})

我的交易详细信息仅包含表格 还有我的 Controller

public function getDetail(){
    $data['temporary'] = $this->mymodel->getAllData('temporary')->result();
    $data['limit'] = $this->mymodel->countAll('temporary');
    $this->load->view('transaction/transaction_detail', $data);
}

最佳答案

您似乎正在尝试使用 jQuery 和 CodeIgniter 在 div 内加载 HTML View 。我会利用 $.ajax method$.get method由 jQuery 提供,因为您已经在使用它。

JavaScript 代码示例如下:

 // var site_url returns your codeigniter application's URL by splitting it before index.php.
// if you're using .htaccess or mod_rewrite for pretty urls (no index.php, you may need to find another way to get the URL of your site)

var site_url = window.location.protocol + '//' + window.location.hostname + window.location.pathname.split('index.php')[0] + 'index.php';

//do a GET request for the transaction/getDetail endpoint
$.get(site_url + 'index.php/transaction/getDetail', function(html) {
    //load the HTML returned by the codeigniter controller into the #t_detail element
    $("#t_detail").html(html);
});

如果您认为这可以解决您的问题,请标记为已接受的答案。

关于javascript - Codeigniter 3 上的 loadData() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253082/

相关文章:

从mysql获取数据到android的PHP脚本

php - 数组值字符串编辑和循环与其他数组和循环相结合

javascript - 如何在删除元素时保持选择框的宽度不变?

javascript - 图像从java servlet到另一个jsp

c# - Windows 运行时从 WebView 中的 JavaScript 访问文件

php - 如何按年份添加和显示值

php - 通过 AJAX 请求从 MySQL 数据库获取大数据

php - 即使在 codeigniter 中语句为 False 时查询返回数据

php - CodeIgniter 表单验证 valid_email 不工作

javascript - Phonegap iOS 应用程序中的 Cookie