使用Ajax在PHP中发表评论
我知道这里已经发布了一些这些问题,但是我无法找到解决问题的方案。 我对任何javascript / ajax都很糟糕,而且我很难尝试让我的ajax代码正常工作。
我在我的网站的视频页面上构建了一个简单的评论系统。 我已经用php构建了评论系统,但是现在我想要获得更多进展并通过ajax将数据发布到我的comments.php文件中。 我想要动态显示评论而不刷新页面。 我已经阅读了一些教程,但由于某种原因,每当我尝试使用js时,我都会感到困惑。 我认为这是语法:S。 我会在下面发布我的代码,如果有人能告诉我我哪里出错了,这将是一个巨大的帮助!
videos.php
// comment box
if($user->isLoggedIn()){
//login to comment
} else { ?>
<div id="user-comment" class="comment-post">
<form id="comment-form" method="post">
<textarea id="comment_body" name="comment"> </textarea>
<input id="submit-btn" type="submit" name="comment_post" value="Post Comment" >
// csrf-token generator
<input type="hidden" id="auth-token" value="<?php echo token::generate(); ?>"></input>
</form>
</div>
//post comments
<div id="comments_post" class="comments-box">
<?php
$get_comments = // query the db here
if(!$get_comments->results()){ ?>
//no comments...
<?php
} else {
foreach ($get_comments->results() as $comment) { ?>
<div class="comment-header">
<?php echo $comment->username . ' | ' . $comment->added;
if ($user == $comment->user OR $user->hasPermission('admin')) { ?>
<a href="delete-comment.php"><i class="fa fa-trash-o onl-link-icon text-right"></i></a>
<?php
}
?>
</div>
<div class="comment-body">
<p><?php echo $comment->comment; ?></p>
</div>
<?php
}
}
?>
</div>
ajax请求
<script type="text/javascript">
$(document).ready(function(){
//Post Comment
$("#submit-btn").on('.submit','click',function(){
var body = $('#comment_body');
$.ajax({
url: 'comments.php',
type: 'post',
async: false,
data:{
'comment_body' : body
},
success:function(){
$('#comment-box').toggleClass("comment-hide");
}
});
});
});
</script>
的comments.php
if($_POST['auth_token'] === session::get('access_token')){
if(isset($_POST['comment_body'])) {
$validate = new validate();
// Validate Data from $_POST
$validation = $validate->check($_POST, array(
'comment_body' => array(
'name' => '<b>Comments</b>',
'required' => true,
'str_min' => 1,
'str_max' => 400,
'comment_filter' => true,
'sql_safe' => true
),
));
if($validation ->passed()){
$comment = escape($_POST['comment']);
try {
$user->create('video_comments', array(
'comment' => $comment,
'user_id' => $user->id,
'video_id' => $video,
'username' => $user->username,
'added' => date('Y-m-d H:i:s')
));
} catch(Exception $e) {
die($e->getMessage());
}
redirect::to($page);
} else {
session::flash('comment_error', 'Comment Failed');
redirect::to($page);
die();
}
} else { redirect::to(404); }
} else { redirect::to(404); }
更新#1控制台错误告诉我这个:
GET(localhost / WEBSITES / myvideosite / css / homepage.css) - 加载资源失败:服务器响应状态为404(未找到)
它指向我的jquery <script src="js/jquery-1.11.3.min.js"></script>
文件,这是在那里挑衅?
Sucess!
经过漫长的一天研究和尝试不同的事情,我终于得到它的工作!
<script type="text/javascript">
$(document).ready(function(){
$("#submit-btn").click(function(){
var body = $('#comment_body').val();
var token = $('#auth-token').val();
if(body== null)
{
window.alert('Please enter a comment.');
}
else
{
$.ajax({
type: 'POST',
url: 'comment.php',
async: false,
data:{
'auth_token' : token,
'comment_body' : body
},
success: function(result){
//alert(result);
$('.comment-post').toggleClass("comment-hide");
$('.comment-sucess-hide').toggleClass("comment-sucess");
$('#comments_post').load(document.URL + ' #comments_post');
}
});
}
return false;
});
$('.del-com').click(function(){
var comid = $(this).attr('id');
$.ajax({
url: 'comment.php',
type: 'POST',
async: false,
data:{
'rmv' : comid
},
success:function(){
$('#comments_post').load(document.URL + ' #comments_post');
}
});
});
});
</script>
如果有人有一些更好的建议,请随时分享,因为我是一个真正的新手与JS和真的想改善。 感谢所有分享的评论。
链接地址: http://www.djcxy.com/p/45967.html上一篇: Posting a comment in PHP with Ajax
下一篇: ASP.NET Web API error “Uncaught SyntaxError: Unexpected token :”