Php Ajax代码在不应该运行的情况下运行错误
我试图做一个注释系统,但我的代码似乎在运行错误,而它不应该,有人知道为什么吗?代码如下: php我使用ajax从另一个文件调用它,该文件编写得很好,它还具有jquery/ajax/etc库:Php Ajax代码在不应该运行的情况下运行错误,php,ajax,Php,Ajax,我试图做一个注释系统,但我的代码似乎在运行错误,而它不应该,有人知道为什么吗?代码如下: php我使用ajax从另一个文件调用它,该文件编写得很好,它还具有jquery/ajax/etc库: <?php include 'inc/dbc.php'; include 'inc/functions.php'; if(isset($_GET['user']) && !empty($_GET['user'])) { $username =
<?php
include 'inc/dbc.php';
include 'inc/functions.php';
if(isset($_GET['user']) && !empty($_GET['user'])) {
$username = $_GET['user'];
} else {
$username = $_SESSION['user'];
}
$my_name = $_SESSION['username'];
if (loggedIn() == true) {
$chech_posts = mysqli_query($mysqli, "SELECT * FROM wallposts WHERE posted_to = '$username' AND deleted = 0 ORDER BY posted_date DESC, posted_time DESC ");
while ($row = mysqli_fetch_array($chech_posts, MYSQLI_BOTH)) {
$post_id = $row['post_id'];
$postby = $row['posted_by'];
$postto = $row['posted_to'];
$post = $row['post'];
$post_date = $row['posted_date'];
$post_time = $row['posted_time'];
$p_avatar = getuser($postby, 'avatar');
$p_first = getuser($postby, 'firstname');
$p_last = getuser($postby, 'lastname');
?>
<div class='wi wall' id='<?php echo $post_id;?>'>
<div class='post'>
<div class='post-container'>
<div class='post-header'>
<div class='pull-left'>
<?php
if ($postby == $postto) {
echo '<b>'.$postby.'</b>';
} else {
echo '<b>' . $postby . '</b> <i class="fa fa-chevron-right"></i> <b>' . $postto . '</b>';
}
?>
</div>
<div class='post-img'><img src="images/users/<?php echo $p_avatar;?>" alt="<?php echo $p_first . ' ' . $p_last . '\'s Profile Picture' ;?>" class="img-circle" align="middle"></div>
<div class='pull-right'><?php echo $post_date;?></div>
<div class='drop'>
<div class="dropdown">
<button class="btn btn-default dropdown-toggle car" type="button" id="postMenu" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="postMenu">
<?php
if ($username == $my_name) {
?>
<script type="text/javascript">
$(function() {
$('#delete').click(function(e){
e.preventDefault();
var post_id = "<?php echo $post_id;?>";
$.ajax({
type: 'POST',
url: 'post_action.php?action=delete&post_id='+post_id,
data: 'delete',
success: function() {
alert('Successfull deleted post!');
$('#wallpos').load('view.php?user='+user);
},
error: function() {
alert('Post was not deleted!');
}
});
});
});
</script>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" id='delete'><i class='fa fa-trash-o fa-lg'></i> Delete</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Another action</a></li>
<?php
}
?>
<li role="presentation" class="divider"></li>
<li role="presentation" class="dropdown-header">Report As</li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Spam</a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Inappropriate</a></li>
</ul>
</div>
</div>
</div>
<div class='post-body'>
<p><?php echo $post;?></p>
</div>
<div class='post-footer'>
<div class='lk-cmt-shr'>
<div><a href='' onClick="return false;">Like</a></div>
<div><a href='' id='opencomment'>Comment</a></div>
</div>
<span id='comments'>
<span id='com'></span>
<script>
$(function() {
var post_id = "<?php echo $post_id;?>";
$(document).ready(function() {
$.get("loadcomments.php?post_id="+post_id);
});
var refresh = setInterval(function() {
$.get("loadcomments.php?post_id="+post_id);
}, 300000);
});
</script>
<span id='viewcomment'></span>
<div class='cform'>
<script type="text/javascript">
$(function() {
$(document).ready(function(){
var post_id = "<?php echo $post_id;?>";
var commentElement = 'cinput-'+post_id;
var comment = $('#'+commentElement).val();
$('#cinput-<?php echo $post_id;?>').keypress(function(e) {
if (e.which == 13) {
e.preventDefault();
if (comment == '') {
alert('comment cannot be blank');
} else {
$.ajax({
type: 'POST',
url: 'sendcomment.php?post_id='+post_id,
data: $('#cf-'+post_id).serialize(),
beforeSend: function() {
$('#com').html('<i class="fa fa-spinner fa-spin"></li>');
},
success: function(data) {
$('#com').hide();
$("#viewcomment").load("loadcomments.php?post_id="+post_id);
},
error: function(data) {
alert('could not post your comment:' + data);
}
});
}
}
});
});
});
</script>
<form method='post' id='cf-<?php echo $post_id;?>'>
<input type='text' class='form-control' id='cinput-<?php echo $post_id;?>' name='comment-<?php echo $post_id;?>' placeholder='Write a comment...'>
<!-- <input type='submit' name='post-comment' id='pc' value=''> -->
</form>
</div>
</span>
</div>
</div>
</div>
</div>
<?php
}
} else {
echo '<li>You must be <a href="index.php">logged</a> in to view ' . $firstname . '\'s posts.</li>';
}
?>
这是sendcomment.php:
<?php
include 'inc/dbc.php';
include 'inc/functions.php';
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
if(isset($_GET['user']) && !empty($_GET['user'])) {
$username = $_GET['user'];
} else {
$username = $_SESSION['username'];
}
$my_name = $_SESSION['username'];
$post_id = $_GET['post_id'];
if (isset($_POST['comment-'.$post_id])) {
$comment = stripcslashes(mysqli_real_escape_string($mysqli, $_POST['comment-<?php echo $post_id;?>']));
$c_date = date('D j F, Y g:i a');
$c_time = date('g:i:s');
$post_comment = mysqli_query($mysqli, "INSERT INTO wallcomments(wallpost_id, commenter, comment, comment_date, comment_time) VALUES ('$post_id', '$my_name', '$comment', '$c_date', '$c_time')");
if (!$post_comment) {
?>
<script type="text/javascript">
alert('Your comment was not posted');
</script>
<?php
} else {
?>
<script type="text/javascript">
alert('Your comment has been posted');
</script>
<?php
}
}
?>
我认为下面这一行是问题的根源
var comment=$'cinput-'.val;你所做的帮助了我,但现在查询不起作用。没有错误,没有成功消息,数据库保持不变。您的查询值是否正确?只要回显您正在运行的查询,并在sql developer或phpMyAdmin中运行它,它将显示查询是否正确..查询在phpMyAdmin中工作,但在我运行代码时不工作,同样在我的代码中,如果你仔细看,我在cinput和post_id之间添加了一个-但是当我在commentElement中修复这个问题时,我得到的错误注释不能为空。试试吧..我刚刚更新了我的答案。你仍然有问题,通过提醒post_id它是否正在生成来检查。它确实正在生成,我正在使用谷歌的控制台来检查。也没有javascript错误