我使用jquery编写了一个评论系统,但问题是ajax请求没有显示已发布的数据而没有加载页面,尽管数据已在db中成功添加
如何在点击提交按钮后显示发布的数据
JS:
$(document).ready(function(){
$("#submit").click(function() {
var ans_body = encodeURIComponent($("#ans_body").val());
var q_id = $("#q_id").val();
var ans_auth='vista-design';
var dt='2-2-2012';
var ans_rate='0';
var ans_correct='no';
var dataString = 'ans_body=' + ans_body + '&q_id=' + q_id + '&ans_auth=' + ans_auth + '&dt=' + dt + '&ans_rate=' + ans_rate + '&ans_correct=' + ans_correct;
if(ans_body=='')
{
alert('برجاء كتابة الاجابة صحيحة');
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="js/loader.gif" align="absmiddle"> <span class="loading">Loading answers...</span>');
$.ajax({
type: "POST",
url: "includes/submit_comment.php",
contentType: "application/x-www-form-urlencoded;charset=utf-8",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
$('#ans_body').val(ans_body);
$("#ans_body").focus();
$("#flash").hide();
}
});
}
return false;
});
})
PHP:
<?php
include_once('../../includes/config.php');
include_once('../includes/bbParser.php');
if($_POST){
@$q_id=$db->real_escape_string($_POST['q_id']);
$parser=new bbParser();
@$ans_body=$parser->getHtml(htmlentities($_POST['ans_body']));
$ans_auth="vista-design";//$_SESSION['username']
$dt=date("h:i:s d-m-Y");
$ans_rate=0;
$ans_correct="no";
if(!empty($ans_body) ){
$prep_answer=$db->prepare("insert into stack_ans(q_id,ans_body,ans_auth,ans_dt,ans_rate,ans_correct) values(?,?,?,?,?,?)");
$bind_answer=$prep_answer->bind_param('ssssss',$q_id,$ans_body,$ans_auth,$dt,$ans_rate,$ans_correct);
$prep_answer->execute();
}//end empty
}
?>
html + php代码显示评论:
<ol id="update" class="timeline">
<?php
while($ans_row=$get_ans->fetch_assoc()){
$ans_body=$ans_row['ans_body'];
$ans_auth=$ans_row['ans_auth'];
$ans_dt=$ans_row['ans_dt'];
$ans_rate=$ans_row['ans_rate'];
$ans_correct=$ans_row['ans_correct'];
$userid=$ans_row['userid'];
$ans_id=$ans_row['ans_id'];
$profile_pic=$ans_row['profile_pic'];
?>
<li class="box">
<div id="eachans_block">
<div id="ans_corate">
<p id="subans_rate">
<a href="#" class="like_ans_btn" id="<?php echo $ans_id; ?>">
<span class="on_img" id="avoid_font" > <?php echo $ans_rate; ?> </span>
</a>
</p>
<hr />
<p id="subans_correct">
<?php
$quest_orig_id=$ans_row['q_id'];
$yes_status="hash".md5("yes");
$no_status="hash".md5("no");
//if click on non-correct answer->>make it correct
if(isset($_GET['status']) && isset($_GET['ans_id']) && $_GET['status']==$yes_status){
$get_ans_id=htmlspecialchars($_GET['ans_id']);
//check if there is answer with correct one
$get_topic_answers=$db->query("select * from stack_ans where q_id='$q_id' and ans_correct='yes'");
$num_topic_answers=$get_topic_answers->num_rows;
if($num_topic_answers>0){
$db->query("update stack_ans set ans_correct='no' where q_id='$q_id'");
$db->query("update stack_ans set ans_correct='yes' where ans_id='$get_ans_id'");
}else{
$db->query("update stack_ans set ans_correct='yes' where ans_id='$get_ans_id'");
}
header("Location:questions.php?id=".$q_id." ");
}//end if isset
//if click on correct answer->make it non-correct
if(isset($_GET['status']) && isset($_GET['ans_id']) && $_GET['status']==$no_status){
$get_ans_id=htmlspecialchars($_GET['ans_id']);
$db->query("update stack_ans set ans_correct='no' where ans_id='$get_ans_id' ");
header("Location:questions.php?id=".$q_id." ");
}//end if isset
$get_quest_auth=$db->query("select * from stack_ask where id='$q_id'");
$fetch_quest_auth=$get_quest_auth->fetch_assoc();
$quest_author=$fetch_quest_auth['q_auth']; //vista-design
$sess_username="vista-design";//$_SESSION['username']
if($sess_username==$quest_author){
if($ans_correct=='no'){
?>
<a href="questions.php?id=<?php echo $q_id; ?>&ans_id=<?php echo $ans_id; ?>&status=hash<?php echo md5('yes'); ?>"> <i id="false_ans" class="fa fa-check fa-4x"></i></a>
<?php
}
if($ans_correct=='yes'){
?>
<a href="questions.php?id=<?php echo $q_id; ?>&ans_id=<?php echo $ans_id; ?>&status=hash<?php echo md5('no'); ?>">
<i id="true_ans" class="fa fa-check fa-4x"></i>
</a>
<?php
}
}//end if sess
?>
</p>
</div>
<div id="ltansblock">
<p id="subans_body"><?php echo stripslashes($ans_body); ?></p>
<div id="ans_authblock">
<p id="img_side">
<?php $img=$profile_pic;?>
<img width="60" height="60" src="../hire/profile_pic/<?php echo $img; ?>" />
</p>
<p id="subans_auth">
<a href="profile.php?uid=<?php echo $userid; ?>"><?php echo $ans_auth; ?></a>
</p>
<p id="subans_dt"><?php echo $ans_dt; ?></p>
</div>
</div>
</div>
</li>
<?php
}
?>
</ol>
<div id="flash" align="right"></div>
<div id="addCommentContainer">
<form method="post" action="" id="postprojectans" >
<input type="hidden" name="q_id" id="q_id" value="<?php echo htmlspecialchars($_GET['id']); ?>"/>
<textarea name="ans_body" id="ans_body" rows="10" cols="60"></textarea>
<fieldset id="actions">
<input type="submit" class="button" name="sub_ans" id="submit" value="اضافة اجابتك">
</fieldset>
</form>
</div>
</div>
答案 0 :(得分:1)
您要提交您从提交评论中获得的HTML:
success: function(html){
$("ol#update").append(html);
...但是您提交评论的脚本不会返回任何HTML,因此您要附加一个空白文档。
更改该PHP程序,以便将新获取的数据格式化为HTML。