我写了一个posts.php
页面,显示某个帖子及其评论,使用户能够动态地向该帖子添加新评论。
我想使用 Ajax 实现 *"submit_comment"*,但我真的不知道如何在 MVC 中实现。
这是我的 Posts.php:
<script type="application/javascript" src="Ajax/addComment.js"> </script>
src="../views/Ajax/addComment.js"> </script> <title> Posts
(View)</title> </head>
<body> <div id="main"> <div class="container"> <?=$data['header'];?>
<div id="content">
<!-- Main Post -->
<div class="content-background">
<h2> <?=$data['title'];?></h2>
<h4> <?=$data['date'];?> </h4>
<p> <?=$data['content'];?></p>
</div>
<div id="form-content">
<form name="commentForm" method="post">
Enter your name: <input type="text" name="username" id="username"> <br />
Enter your comment: </br> <textarea name="comment" id="comment" cols="10" rows="10"> </textarea> <br />
<input value='Submit' type='button' onclick='JavaScript:commentRequest2("<?=$data['id']?>")'
name='submit'>
</form>
</div>
<div id="commentArea">
<?=include_once("comments_area.php");?>
</div><!-- end commentArea -->
</div> </div> </div>
</body> </html>
这是我的 Posts_Controller.php:
<?php
/**
* This file handles the retrieval and serving of posts posts
*/
class Posts_Controller
{
/**
* This template variable will hold the 'view' portion of our MVC for this
* controller
*/
public $template = 'posts';
/**
* This is the default function that will be called by router.php
*
* @param array $getVars the GET variables posted to index.php
*/
public function main(array $getVars)
{
$postsModel = new Posts_Model;
}
else{
//get the post
$post = $postsModel->get_main_post("`id`='", $getVars['id'], "LIMIT", "1");
//get the comments
$comments = $postsModel->get_comments("`postID`='", $getVars['id']);
//create a new view and pass it our template
$header = new View_Model('header_template');
$view = new View_Model($this->template);
//assign post data to view
$view->assign('header', $header->render(FALSE));
$view->assign('title' , $post['title']);
$view->assign('content' , $post['content']);
$view->assign('date' , $post['date']);
$view->assign('by' , $post['added_by']);
$view->assign('id' , $post['id']);
$view->assign('commentsArr' , $comments);
$view->render();
}
}
}
这是我的 Posts_Model.php:
<?php
/**
* The Posts Model does the back-end heavy lifting for the Posts Controller
*/
class Posts_Model
{
/**
* Holds instance of database connection
*/
private $db;
public function __construct()
{
$this->db = new MysqlImproved_Driver;
}
/**
* Fetches article based on supplied name
*
* @param string $author
*
* @return array $article
*/
public function get_main_post($cond1, $var1, $cond2 ="", $var2 = "")
{
//connect to database
$this->db->connect();
//sanitize data
$var1 = $this->db->escape($var1);
$var2 = $this->db->escape($var2);
$cond = $cond1.$var1."'";
$cond.= " ".$cond2." ".$var2;
//prepare query
$this->db->prepare
(
"
SELECT * FROM `posts`
WHERE $cond
;
"
);
//execute query
$this->db->query();
$article = $this->db->fetch('array');
return $article;
}
public function get_comments($cond1, $var1, $cond2 ="", $var2 = "")
{
//connect to database
$this->db->connect();
//sanitize data
$var1 = $this->db->escape($var1);
$var2 = $this->db->escape($var2);
$cond = $cond1.$var1."'";
$cond.= " ".$cond2." ".$var2;
//prepare query
$this->db->prepare
(
"
SELECT * FROM `comments`
WHERE $cond
;
"
);
//execute query
$this->db->query();
$resultArr[0] = 0;
$i = 1;
while( $result = $this->db->fetch('array')){
$resultArr[$i] = $result;
$i++;
}
$resultArr[0] = $i;
return $resultArr;
}
}
?>
我应该在哪里添加 addComment.js?(V、M 还是 C?) addComment 应该做什么?调用什么 URL/函数?
有什么建议么?例子?