如何使用ajax添加评论? [英] How to add comments live using ajax?

查看:87
本文介绍了如何使用ajax添加评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个用于在帖子中插入评论的PHP代码,现在工作正常我想添加ajax代码来实时添加评论(没有页面被刷新)。我更喜欢同一页面上的ajax代码和php代码,因为代码将携带用户的id,所以不希望通过ajax / js将这些数据发送到另一个页面。我不擅长编程试过几个ajax代码但是失败了所以如果有人可以帮助我使用ajax代码将会很感激。



 < span class =code-pagedirective><?php  
if(isset($ _ POST [' submit'])){
$ comment = strip_tags($ _ POST [' 内容']);
$ com = $ db-> prepare( INSERT INTO comments(评论,用户,用户,博客)VALUES(:comment,:userto,:userfrom,:blog));
$ com->执行(数组(' :comment' => $ comment ,' :userto' => $ userid,' :userfrom' => $ uid,' :博客' => $ blogId));

}
?>



< pre lang =HTML> < 表格 方法 =' 发布' < span class =code-attribute> name =' form' action = ' ' class < span class =code-keyword> =' commentbox2' >
< textarea 名称 =' content' id =' content' > < / textarea > < br / >
< 输入 类型 =' 提交' value =' 评论' name =' submit' class =' comment_button' / >
< / form >

解决方案

_POST [' submit'])){


comment = strip_tags(


_POST [' content']);


Below is a php code for inserting comment to a post, It works fine now I want to add ajax code to add comments live (without page being refreshed). I prefer ajax code and php code on same page because code will be carrying id's of users so don't want to send such data to another page through ajax/js. I am not good at programming tried few ajax codes but failed so if anyone can help me with ajax code will be thankful.

<?php
   if(isset($_POST['submit'])) {
     $comment=strip_tags($_POST['content']);
     $com = $db->prepare("INSERT INTO comments (comment,userto, userfrom, blog) VALUES (:comment, :userto, :userfrom, :blog)");
     $com->execute(array(':comment'=>$comment,':userto'=>$userid,':userfrom'=>$uid,':blog'=>$blogId));

   }
?>


<form method='post' name='form' action='' class='commentbox2'>
<textarea  name='content' id='content'></textarea><br />
<input type='submit' value='Comment' name='submit' class='comment_button'/>
</form>

解决方案

_POST['submit'])) {


comment=strip_tags(


_POST['content']);


这篇关于如何使用ajax添加评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆