我有一个问题。我如何从文件中获得数据时,锚点单击,
这是我在控制器中的功能。
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
参数的set默认值更改为$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自己的方法(NOT CI core)
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
作为演示:
设置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>
标签?新一代的加价?我以前没有看到这个,inline
元素使用<span>
, block-level
元素使用<p>
。
可以使用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/autolload .php
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url', 'form');