javascript:
<script> $(‘.ajax-post‘).click(function(){ var action_url=$(‘form‘).attr(‘action‘); var title=$(‘#title‘).val(); var keyword=$(‘#keyword‘).val(); var url=$(‘#url‘).val(); var pid=$(‘#pid‘).val(); $.ajax({ url:action_url,// 跳转到 action data:{ "title" : title, "keyword" : keyword, "url" : url, "pid" : pid }, type:‘POST‘, cache:false, dataType:‘json‘, success:function(data) { if(data.status =="true" ){ // view("修改成功!"); alert("修改成功!"); window.location.reload(); }else{ view(data.content); } }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus); // paser error; }, }); }); </script>
html form:
<form action="{:addons_url(‘CustomMenu://CustomMenu/add‘)}" method="post" class="form-horizontal"> <div class="form-item"> <label class="item-label">菜单名<span class="check-tips">(微信菜单)</span></label> <div class="controls"> <input type="text" class="text input-large" name="title" value="" id="title"> </div> </div> <div class="form-item"> <label class="item-label">关联关键词<span class="check-tips">(对应微信菜单的click事件)</span></label> <div class="controls"> <input type="text" class="text input-large" name="keyword" value="" id="keyword"> </div> </div> <div class="form-item"> <label class="item-label">关联URL<span class="check-tips">(对应微信菜单的view事件)</span></label> <div class="controls"> <input type="text" class="text input-large" name="url" value="" id="url"> </div> </div> <div class="form-item"> <label class="item-label">上级菜单<span class="check-tips">(所属的上级菜单)</span></label> <div class="controls"> <select name="pid" id="pid"> <option value="0">顶级菜单</option> </select> </div> </div> <div class="form-item"> <input type="hidden" name="id" value=""> <button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">确 定</button> </div> </form>
PHP
public function add(){ if(IS_POST){ $Menu = D(‘CustomMenu‘); $data = $Menu->create(); if($rs = M(‘CustomMenu‘)->add($data)){ $data[‘status‘] = ‘true‘; $data[‘content‘] = ‘添加成功‘; $this->ajaxReturn($data,‘json‘); }else{ $data[‘status‘] = ‘false‘; $data[‘content‘] = ‘添加失败‘; $this->error($data,‘json‘); } } $this->display(‘[email protected]‘); }
时间: 2024-11-07 18:06:12