Home > Article > Backend Development > Code implementation of ajax asynchronously adding data in tp5 framework
The content of this article is about the code implementation of asynchronously adding data in the tp5 framework ajax. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The user experience of ajax asynchronous non-refresh processing of data is still good, and it is a knowledge point that is often used in our project development. So Ajax asynchronous addition is made. What we usually use may be Ajax asynchronous deletion and asynchronous modification. Asynchronous addition may be slightly less. Let’s take a look at our processing logic and core code.
This is the interface for adding our data:
The front-end js code is as follows:
[js] <script type="text/javascript"> function adddhm(){ var oid=$("input[name='oid']").val(); var uname=$("input[name='uname']").val(); var dhm=$("input[name='dhm']").val(); var _class=$("select[name='class']").val(); var otime=$("input[name='otime']").val(); if(!oid){ alert('订单号不能为空!'); return false; } if(!uname){ alert('用户名不能为空!'); return false; } if(!dhm){ alert('兑换码不能为空!'); return false; } if(!_class){ alert('适用课程不能为空!'); return false; } if(!otime){ alert('下单时间不能为空!'); return false; } $.ajax({ type:"POST", data:{oid:oid,uname:uname,dhm:dhm,class:_class,otime:otime}, url:"{:url('add')}", success:function(data){ alert(data.msg); } }); $("#myform")[0].reset() ; return false; } </script> [/js]
The following is what we add after receiving the data Logic:
[php] public function add() { if(request()->isPost()){ $_data=input('post.'); $data=array(); foreach ($_data as $k => $v) { $data[$k]=trim($v); } $validate = validate('dhm'); if(!$validate->check($data)){ $msg=$validate->getError(); return json(['error'=>2,'msg'=>$msg]); } $add=db('dhm')->insert($data); if($add){ return json(['error'=>0,'msg'=>'添加成功!']); }else{ return json(['error'=>1,'msg'=>'添加失败,请重新添加!']); } return; } return view(); } [/php]
Related recommendations:
tp5 method code for batch uploading pictures
##################### ######The above is the detailed content of Code implementation of ajax asynchronously adding data in tp5 framework. For more information, please follow other related articles on the PHP Chinese website!