douqiang5933 2016-03-27 12:17
浏览 49

在PHP中发布和评论功能

I am creating a post and comment functionality in my website. I am using Ajax and PHP for the purpose. But there seems to be a bit problem. When ever I add a comment, it is always added to the first post. I have to reload the page to get all the comments in their respective posts. I created the ajax code to prevent the dependence on reloading, but it seems like ajax is not working correctly.

<script type="text/javascript">
function postComment(id) {
    var cuser = $("#uid" + id).val();
    var cdetail = $("#detail" + id).val();
    var cpost = $("#post" + id).val();
    var dataString = 'cuser='+ cuser + '&cdetail=' + cdetail + '&cpost=' + cpost;

    if (cdetail == '') {
        alert('Please Give Valid Details');
    } else {
        $.ajax({
            url:  "index.php?mod=Comment&do=comment",
            data: dataString,
            type: "POST",
            success: function(resp) { 
                document.getElementById('rslt').innerHTML = resp;
            },
            error: function(e) {  
                alert('Error: '+ JSON.stringify(e)); 
            }  
        });
    }
}
</script>

<div align="center" style="background-color:#f6f7f8;">
   <table width="98%">
      <form method="post">
         <tr>
            <td>
               <textarea rows="1" id="detail<?php echo $i ?>" class="form-control" name="detail" style="width:100%; resize:none;" placeholder="Add comment"></textarea>
            </td>
         </tr>
         <tr>
            <td align="center">
               <input type="hidden" id="uid<?php echo $i ?>" name="uid" value="<?php echo $uid ?>">
               <input type="hidden" id="post<?php echo $i ?>" name="post" value="<?php echo $getPosts_pId ?>">

               <input type="button" onClick="postComment('<?php echo $i ?>')" class="btn btn-sm btn-primary pull-left margin-top" name="comment" value="Comment">
            </td>
         </tr>
      </form>
   </table>
</div>


<div align="left" style="margin-bottom:10px;">
 <?php 
 //Getting all the comments corresponding to a particular post
 $getComment_cPost = $getPosts_pId;
 $getComment->execute();
 $getComment->bindColumn("comment_id", $getComment_cId);
 $getComment->bindColumn("comment_user", $getComment_cUser);
 $getComment->bindColumn("comment_detail", $getComment_cDetail);

 while ($getComment->fetch()) {
 //Getting only the comments written by a particular user
 $uid = $getComment_cUser;
 $getUser->execute();
 $getUser->bindColumn('user_fname', $cfName);
 $getUser->bindColumn('user_lname', $clName);
 $getUser->fetch();
 ?>
 <div style="background-color:#f6f7f8;">
 <font style="font-size:14px; color:#4c5d98"><b><?php echo "@" . $cfName . " " . $clName ?></b></font>
 </div>
 <div style="background-color:#f6f7f8;">
 <font style="font-size:14px;"><?php echo $getComment_cDetail ?></font>
 </div>
 <div style="margin-bottom:10px; background-color:#FFF;">
 </div>
 <?php 
 } 
 ?>
 <div id="rslt">
 </div>
</div>

I have created a variable i and am incrementing it accordingly to assign every comment a seperate id otherwise the values won't submit.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多
    • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
    • ¥30 ppOCRLabel导出识别结果失败
    • ¥15 Centos7 / PETGEM
    • ¥15 csmar数据进行spss描述性统计分析
    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题