请看一下这段代码:
$('ul#navigation li ul li>a').click(function(){
var active = $(this).text();
$('#artcontent p').empty();
$.ajax({
type: 'POST',
url: 'homepage/readarticle',
data: $(active).serialize(),
success: function(databack){
$('#loading').fadeOut('normal');
$('#artcontent').append(databack);
}
})
});
function readarticle()
{
$articlename = $this->input->post('active');
$output = $this->articles->displayby_name($articlename);
if($output){
$data['article'] = $output;
}
$this->load->view('loadarticle',$data);
}
这是服务器上的函数。
我有一个 ul
li
a,它是父 ul
导航和 li
的子级。我想做的是加载文章页面。这是可行的,但它会读取数据库并加载我的数据库中的所有文章,而不是由 active 标识的文章。我想知道我做错了什么或者有更好的方法来解决这个问题吗?
好的,“homepage/readarticle”是我的 CodeIgniter Controller 中的“ Controller /方法”,它加载呈现文章的 View 。
至于序列化行,我实际上在没有序列化的情况下将其置于事件状态,并且可以使用通常的 console.log()
捕获 Firebug 的链接文本。就像我说的,它可以读取文章,但是它从我的数据库中读取所有文章,而不是获取标题等于事件变量中的名称的文章(这将是链接的文本,例如, 看这个。看这是正文。)
好的,让我添加处理这个问题的代码,该代码也来自模型的最后一部分:
<?php
function displayby_name($name) { $this->db->select("articletitle,articlebody,articleauthor");
$this->db->from('articles'); $this->db->where('articletitle',$name);
$Q = $this->db->get(); if($Q->num_rows() > 0){ $text = $Q->result_array(); }
echo $this->db->last_query();
return $text; }
?>
还有什么想法吗?
最佳答案
$(active).serialize()
将产生空字符串。您可能想要删除 data: $(active).serialize(),
行并执行类似 url: 'homepage/readarticle/' +encodeURIComponent(active),
,取决于您的服务器端应用程序的期望。
编辑:
根据您刚刚添加到问题中的服务器端代码,如果将 data: $(active).serialize(),
替换为 data: {active,您的代码应该可以工作:活跃},
。
另一个编辑:
好吧,我冒昧地尝试重新创建您的设置。我的代码运行良好(据我了解您的预期行为)。
以下是我的 CodeIgniter 应用程序。通读它并尝试运行它(如果可以的话)。如果没有帮助,请添加评论,我可以从那里为您提供帮助,因为我现在已经完成了设置。
+---controllers
| homepage.php
|
+---models
| articles.php
|
\---views
homepage.php
loadarticle.php
Controller \homepage.php:
<?php
class Homepage extends Controller {
function Homepage()
{
parent::Controller();
$this->load->model('articles');
}
function index()
{
$output = $this->articles->display_all();
$data['articles'] = $output;
$this->load->view('homepage', $data);
}
function readarticle()
{
$articlename = $this->input->post('active');
$output = $this->articles->displayby_name($articlename);
if($output){
$data['articles'] = $output;
}
$this->load->view('loadarticle', $data);
}
}
模型\文章.php
<?php
class Articles extends Model {
function Articles()
{
parent::Model();
}
function display_all() {
$this->db->select("articletitle");
$this->db->from('articles');
$Q = $this->db->get();
$results = array();
if ($Q->num_rows() > 0) {
$results = $Q->result_array();
}
return $results;
}
function displayby_name($name) {
$this->db->select("articletitle, articlebody, articleauthor");
$this->db->from('articles');
$this->db->where('articletitle', $name);
$Q = $this->db->get();
if ($Q->num_rows() > 0) {
$text = $Q->result_array();
}
return $text;
}
}
View \homepage.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Homepage</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<style type="text/css" media="screen">
#navigation { float: left; padding: 1em; }
#loading {
display: block; position: absolute;
top: 0; left: 50%; display: none; width: 8em; margin-left: -4em;
}
#artcontent { padding: 2em; }
</style>
</head>
<body>
<ul id="navigation">
<li>
<span>Articles:</span>
<ul>
<?php foreach ($articles as $article): ?>
<li><a href="#"><?php echo $article['articletitle']; ?></a></li>
<?php endforeach; ?>
</ul>
</li>
</ul>
<span id="loading">Loading...</span>
<div id="artcontent"><p></p></div>
<script>
$('ul#navigation li ul li>a').click(function(){
var active = $(this).text();
$('#artcontent').empty();
$('#loading').show();
$.ajax({
type: 'POST',
url: 'homepage/readarticle',
data: {active: active},
success: function(databack){
$('#loading').fadeOut('normal');
$('#artcontent').append(databack);
}
});
return false;
});
</script>
</body>
</html>
View \loadarticle.php
<?php foreach ($articles as $article): ?>
<h2><?php echo $article['articletitle']; ?></h2>
by <span><?php echo $article['articleauthor']; ?></span>
<p><?php echo $article['articlebody']; ?></p>
<?php endforeach; ?>
MySQL结构和示例数据
CREATE TABLE IF NOT EXISTS `articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`articletitle` varchar(200) NOT NULL,
`articlebody` text NOT NULL,
`articleauthor` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `articles` (`id`, `articletitle`, `articlebody`, `articleauthor`)
VALUES
(1, 'foo', 'body foo body', 'author foo author'),
(2, 'bar', 'body bar body', 'author bar author'),
(3, 'baz', 'body baz body', 'author baz author');
关于php - jQuery 和 Ajax,仅从数据库加载一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1534370/