[英]Retrieving File Data in CodeIgniter by clicking on an anchor tag via AJAX
我有这个问题。 单击锚点后如何从文件中获取数据,
这是我在控制器中的功能。
function change_log_year_view($year) {
$file = $this->get_file($year);
$message = $this->generate_message_body(read_file($file));
if (!empty($year)) {
$data['id'] = $this->session->userdata('id');
$data['message'] = $message;
$this->load->view('change_log_view', $data);
}
这是视图。
echo anchor('changelog/change_log_year_view/' . '2013', 'Change Log 2013');
echo '|' . anchor('changelog/change_log_year_view/' . '2014', 'Change Log 2014');
echo '|' . anchor('changelog/change_log_year_view/' . '2015', 'Change Log 2015');
<text style="white-space: pre-line;" id="message"><?php echo $message; ?></text>
这种方式有效。 但是,如果我使用jquery或javascript,这是可能的吗?如果可能,脚本的外观如何,谢谢。
首先,将$year
参数的默认值更改为$year = ''
以防止出错。
基本上,您的控制器应回显数据,而不返回。 因此,直接回显$message
。
echo $this->generate_message_body(read_file($file));
PHP (控制器文件) :
function change_log_year_view($year = '')
{
if (empty($year)) {
die('Optional Error Message...');
}
$file = $this->get_file($year);
$message = $this->generate_message_body(read_file($file));
// Is this necessary? I assume NO,
// because you didn't show any usage of $id variable in your question.
// $data['id'] = $this->session->userdata('id');
// $data['message'] = $message;
// $this->load->view('change_log_view', $data);
echo $message;
}
注意:
$this->get_file()
和$this->generate_message_body()
是OP自己的方法( 不是 CI核心)
JS ( 使用jQuery ) :
$.ajax({
type : 'GET', // or 'POST', whatever you want.
dataType : 'text', // output_value will be a plain text string.
url : 'YOUR_ANCHOR_TAG_URL',
success : function(output_value){
// output_value is the result.
// do what you want with it.
}
});
注意:
插入您的控制器URL地址(不要忘记YEAR
变量),而不要插入YOUR_ANCHOR_TAG_URL
短语。
您可以通过$('your_link_selector').attr('href');
自动获取URL $('your_link_selector').attr('href');
将class="ajax-call"
为您的<a>
标签。
$('.ajax-call').on('click', function(e) {
e.preventDefault();
var _this = $(this);
$.ajax({
type : 'GET',
dataType : 'text',
url : _this.attr('href'),
success : function(output_value){
// output_value is the result.
// do what you want with it.
}
});
}
附带提一个问题:什么是<text>
标记? 新一代的标记? 我之前没有看到过,将<span>
用于inline
元素,将<p>
用于block-level
。
可以使用jQuery ajax完成。 您可以使用ajax调用控制器方法。
您可以像这样在CI中使用Ajax:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ajax with CI</title>
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('.ci_link').on('click',function(){
// get link href string
var linkHref = $(this).attr('href');
// get year from string
var getYearFromLinkHref = linkHref.split('/')[2];
// your ajax param to send CI Controller
var sendAjaxDataToController = {year:getYearFromLinkHref};
// if you are using CSRF token in CI then
// token name CI Function
var csrftokenname = "<?php echo $this->security->get_csrf_token_name()?>";
// token value CI Function
var csrftokenvalue = "<?php echo $this->security->get_csrf_hash();?>";
var sendAjaxDataToController = {year:getYearFromLinkHref,csrftokenname:csrftokenvalue};
// Ajax Code Start
$.ajax({
type: "POST",
data: $.param(sendAjaxDataToController ),
url: "http://www.yoursite.com/Changelog/change_log_year_view",
success: ajaxSucceess,
error: ajaxError
});
// Ajax Code End
// Code for Success Handler
function ajaxSucceess(response) {
console.log(response);
}
// Code for Error Handler
function ajaxError(response) {
console.log(response.status + ' ' + response.statusText);
}
});
});
</script>
</head>
<body></body>
</html>
//您的控制器代码
<?php
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Changelog extends CI_Controller {
/**
* Chnage Log Year Function
*/
function change_log_year_view() {
// get year data from ajax post
$year = $this->input->post('year');
$file = $this->get_file($year);
$message = $this->generate_message_body(read_file($file));
if (!empty($year)) {
$data['id'] = $this->session->userdata('id');
$data['message'] = $message;
$this->load->view('change_log_view', $data);
}
}
?>
//您的锚定代码应如下所示:
<?php
// create array attribute for link
$linkAttr = array('title'=>'your link title','class'=>'ci_link');
// show a link with attribute
echo anchor('changelog/change_log_year_view/2013', 'Change Log 2013', $linkAttr);
?>
//您的CI应用程序/config/autoload.php
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url', 'form');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.