本文实例为大家分享了微信小程序实现评论功能的具体代码,供大家参考,具体内容如下
前端
<textarea class='the_prw_in' bindinput='bindblur' cursor-spacing="130" placeholder='说点什么吧...' maxlength="76"> </textarea> <view class='the_prw_btn' bindtap='btn_send'> 留言 </view> <view>评论内容:</view> <block wx:for="{{pl_list}}" wx:key="index"> <view class='the_msg' wx:if='{{item.input_value!=null}}'> <!-- <view class='msg_left'> <view class='msg_avatar_v'> <image class='msg_avatar' src='{{msg_data.avatar}}'></image> </view> </view> --> <view class='msg_right'> <!-- <view class='msg_right_name'> {{msg_data.nicename}} </view> --> <view class='msg_right_text'> <text>{{item.input_value}}</text> </view> <view class='gap'> </view> </view> </view> </block>
js: 默认展示历史评论,评论后也刷新页面,连带此次评论内容一起展示。
var bindblur ; page({ bindblur:function(e){ console.log('1111111:', e.detail.value) bindblur = e.detail.value; }, onload: function (a) { var that = this; actid = a.id; // 查询评论fetch wx.request({ url: 'https://m.yishushe.net/addons/up_text.php', method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded', 'accept': 'application/json' }, data: { act_id: actid }, success: function (result) { that.setdata({ pl_list: result.data.reverse(), }) }, fail: res => { wx.showtoast({ title: '网络不好哟', image: '/image/wrong.png', duration: 3000 }) } }) }, btn_send: function () { var that = this //添加评论 console.log('文章id:act_id :', actid); console.log('用户缓存id:user_id :', user_id); console.log('文本输入框: input_value :', bindblur); wx.request({ url: 'https://m.yishushe.net/addons/up_text.php', method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded', 'accept': 'application/json' }, data: { act_id: actid, user_id: user_id, input_value: bindblur }, success: function (result) { that.setdata({ pl_list: result.data.reverse(), input_value: "", }) }, fail: res => { wx.showtoast({ title: '网络不好哟', image: '/image/wrong.png', duration: 3000 }) } }) } })
后端php 源码:
<?php header("content-type:text/html;charset=utf8"); header("access-control-allow-origin: *"); //解决跨域 header('access-control-allow-methods:post');// 响应类型 header('access-control-allow-headers:*'); // 响应头设置 $link=mysql_connect("localhost","root","root"); mysql_select_db("weiqing", $link); //选择数据库 mysql_query("set names utf8");//解决中文乱码问题 //$username = $_post['username']; //$avatarurl = $_post['avatarurl']; $act_id = $_post['act_id']; if($_post['input_value']){ $user_id = $_post['user_id']; $input_value = $_post['input_value']; //echo $avatarurl."----time:". $time."----iv:".$iv."----inputvalue:". $inputvalue; //插入数据到数据库 $strsql = "insert into pinglun (act_id,user_id,input_value) values('$act_id','$user_id','$input_value')"; //mysql_query() 函数执行一条 mysql 查询。select,show,explain 或 describe 都需要用这个函数执行 $result = @mysql_query($strsql); } $q = "select * from pinglun"; //sql查询语句 select * from 表名 $rs = mysql_query($q); //获取数据集 if(!$rs){die("数据库没有数据!");} //循环读取数据并存入数组对象 $dlogs;$i=0; while($row=mysql_fetch_array($rs)) { if($act_id ==$row["act_id"]){ $dlog["act_id"]=$row["act_id"]; $dlog["user_id"]=$row["user_id"]; $dlog["input_value"]=$row["input_value"]; } //$dlog["avatarurl"]=$row["avatarurl"]; //$dlog["username"]=$row["username"]; $dlogs[$i++]=$dlog; } //以json格式返回html页面 echo urldecode(json_encode($dlogs)); ?>
如果php返回报错就找到php-ini 配置文件 ,把
display_errors = on
改为
display_errors = off
禁止php报错
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
offset、client、scroll (width,height、left,top、X,Y)
网友评论