客户端的html+js代码
<script>
function aa(){
var elements = document.forms['message'].elements;
var content = elements.content.value;
if(content == "") {
alert('评论内容不能为空');
elements.content.focus();
return false;
}
url="/qt_ajax_wx/get_page_wx/";
$.ajax({
url : url,
data: $('#pinglun_form').serialize(),
dataType : 'json', //服务器返回json格式数据
type : 'post',
cache: false,//不缓存
success : function(datas) {
html='<p>您的留言内容是:'+datas.content+'留言网址是'+datas.url+'</p>';
$("#pinglun").append(html);
}
});
}
</script>
<input type="button" onClick="aa()" value="请求测试" />
<form method="post" name="message" id="pinglun_form" >
<textarea name="content" placeholder="发表神评妙论" contenteditable="true"></textarea>
<input name="articleid" type="hidden" value="34" />
<input name="type" type="hidden" value="1" />
<input name="url" type="hidden" value="<?='https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']?>" />
</form>
<div id="pinglun"></div>
服务器端php代码
public function get_page_wx(){
$data=$this->input->post();
if($data['type']=='1'){
$return['content']=$data['content'];
$return['url']=$data['url'];
}else{
//nothing to do
}
exit(json_encode($return));//返回json数据
}