如何在MVC-php博客中使用AJAX动态添加注释
我编写了一个如何在MVC-php博客中使用AJAX动态添加注释,php,ajax,model-view-controller,Php,Ajax,Model View Controller,我编写了一个posts.php页面,其中显示了某篇文章及其评论,使用户能够动态地向这篇文章添加新的评论 我想用Ajax实现*“submit_comment”*,但我真的不知道如何在MVC中实现它 这是我的Posts.php: <script type="application/javascript" src="Ajax/addComment.js"> </script> src="../views/Ajax/addComment.js"> </script&
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>
src=“../views/Ajax/addComment.js”>帖子
(视图)
输入您的姓名:
输入您的评论:
这是我的帖子。\u 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();
}
}
}
在控制器中创建一个函数,允许您将javascript文件添加到视图中。然后在控制器中调用它。它会将文件路径添加到数组中,并在显示视图之前将其指定给模板
$view->addJavascript('/Ajax/addComment.js');
或者你可以用
$js_files[] = '/Ajax/addComment.js';
$view->assign('js_files', $js_files);
那么在你看来,你可以使用
<?
foreach($data['js_files'] as $file)
{
echo '<script type="application/javascript" src="'.$file.'"> </script>'
}
?>
不确定您的路由在应用程序中是如何构造的,但您可以创建一个类似于
主要用于ajax功能。若你们提到更多关于URL如何路由到控制器的信息,那个么我可能会提出一个更有教育意义的建议
在您尝试学习如何使用MVC之前,您应该学习如何编写语义HTML和不引人注目的JavaScript!从posts.php
文件判断,您确实不知道自己在做什么
我建议你阅读所有的东西。它将包含许多关于HTML的非常好的材料。然后看,然后看一切。我还建议你们读这本书。
您使用的是自制框架还是第三方产品
无论如何。您应该首先创建一个非AJAX方法来添加注释。在当前设置中,最有可能通过在Posts\u Model
中添加post\u comment()
方法来实现。然后在Posts\u Controller
中创建一个单独的方法,用于处理POST
请求
完成后,您可以开始使用AJAX为文章发表新的评论。您的'/views/Ajax/addComment.js'
文件应该添加到posts.php
文件中(您称之为“视图”,但它不是)。您应该使用绝对路径来包含该JS文件
AJAX请求应该通过事件处理程序执行,该事件处理程序附加到
的onsubmit
事件。从表单元素收集数据并创建XHR请求。要了解如何做,请阅读
也就是说,您的代码容易受到SQL注入的攻击。仅仅因为您使用PDO/MySQLi,它就不会神奇地保护您。实际上,你也需要知道如何使用它。我会推荐阅读(是的,这是针对PDO的,但理论不会改变)。尤其地它应该向你解释如何正确使用准备好的陈述,以及为什么它很重要。对于MySQLi等价物,您必须阅读
<?
foreach($data['js_files'] as $file)
{
echo '<script type="application/javascript" src="'.$file.'"> </script>'
}
?>