Home  >  Article  >  Backend Development  >  Code implementation of ajax asynchronously adding data in tp5 framework

Code implementation of ajax asynchronously adding data in tp5 framework

不言
不言Original
2018-08-20 16:41:093019browse

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:

Code implementation of ajax asynchronously adding data in tp5 framework

The front-end js code is as follows:

[js]
<script type="text/javascript">
 function adddhm(){
 var oid=$("input[name=&#39;oid&#39;]").val();
 var uname=$("input[name=&#39;uname&#39;]").val();
 var dhm=$("input[name=&#39;dhm&#39;]").val();
 var _class=$("select[name=&#39;class&#39;]").val();
 var otime=$("input[name=&#39;otime&#39;]").val();
 if(!oid){
 alert(&#39;订单号不能为空!&#39;);
 return false;
 }
 if(!uname){
 alert(&#39;用户名不能为空!&#39;);
 return false;
 }
 if(!dhm){
 alert(&#39;兑换码不能为空!&#39;);
 return false;
 }
 if(!_class){
 alert(&#39;适用课程不能为空!&#39;);
 return false;
 }
 if(!otime){
 alert(&#39;下单时间不能为空!&#39;);
 return false;
 }
 $.ajax({
 type:"POST",
 data:{oid:oid,uname:uname,dhm:dhm,class:_class,otime:otime},
 url:"{:url(&#39;add&#39;)}",
 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(&#39;post.&#39;);
 $data=array();
 foreach ($_data as $k => $v) {
 $data[$k]=trim($v);
 }
 $validate = validate(&#39;dhm&#39;);
 if(!$validate->check($data)){
 $msg=$validate->getError();
 return json([&#39;error&#39;=>2,&#39;msg&#39;=>$msg]);
 }
 $add=db(&#39;dhm&#39;)->insert($data);
 if($add){
 return json([&#39;error&#39;=>0,&#39;msg&#39;=>&#39;添加成功!&#39;]);
 }else{
 return json([&#39;error&#39;=>1,&#39;msg&#39;=>&#39;添加失败,请重新添加!&#39;]);
 }
 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn