ajax的作用就是 在页面不刷新的情况下 后台提交数据 返回结果。
1、需要提交的表单内容
<form method ="Post" id ="formid" onsubmit="return false"> <input type="text" name="to" class="editor-text " placeholder="请填写你爱的人"> <input type="submit" onclick="sub()" value="提交" </form>
onsubmit="return false" 作用:form 加上属性 onsubmit="return false",可以阻止form提交导致自动刷新(即无刷新提交)
2、后端js Ajax的处理
<script type="text/javascript"> function sub() { $.ajax({ type: "POST", url:"{#WWW}Wall/Wallpost", dataType:"json", data:$("#formid").serialize(),// 你的formid success: function(e) { alert(e.info); }, error: function(e) { alert("发布成功");window.location.href="{#WWW}"; } }); return false; } </script>
3、Ajax返回return
if(empty($wallcontent)) return $this->json(array('error'=>false,'info'=>'表白内容不能为空'));
看没看