【发布时间】:2016-10-07 09:39:02
【问题描述】:
我知道这里已经发布了一些这样的问题,但我无法找到解决问题的方法。我对任何 javascript/ajax 都非常糟糕,我在尝试让我的 ajax 代码工作时遇到了困难。
我正在为我的网站的视频页面构建一个简单的评论系统。我已经用 php 构建了评论系统,但现在我想更进一步并通过 ajax 将数据发布到我的 cmets.php 文件中。我希望 cmets 在不刷新页面的情况下动态显示。我已经阅读了一些教程,但是由于某种原因,每当我尝试使用 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>
cmets.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> 文件,它绝对存在?
【问题讨论】:
-
错误是什么?
-
嗨 @Caio 我在控制台日志中收到这些错误:GET localhost/WEBSITES/myvideosite/css/homepage.css (index):1 Uncaught SyntaxError: Unexpected token
-
它说我的 有语法错误:S
-
我们在您的控制台内没有可看到的水晶球。因此,如果您愿意在此处发布您遇到的错误,那将很难为您提供帮助。
标签: javascript php jquery ajax forms