Home > Article > Backend Development > Xin Xing briefly analyzes the implementation process of ajax
Speaking of ajax, it is definitely a commonplace topic. The use of ajax technology has become quite popular in recent years. Let's take jQuery as an example to see an ajax example from a real project.
The first is the front-end page. On this page we use bootstrap and several of our own technologies. Finally, we have a button whose source code is as follows:
$deal .= '<a href="javascript:void(0);" class="btn btn-danger btn-mini" /a>';
I believe you have a js foundation Friends, it is not a problem to understand this code. Here are a few points to explain:
1. It uses bootstrap, so we will see things like btn btn-danger btn-mini,
2. It calls a deleteOne() function, and the parameter $Id in this function is a variable in PHP.
Then there is the code for the deleteOne method. Note that here we have imported the jQuery library, and of course there are some other libraries. Here are only the more important parts of the code:
<script type="text/javascript"> var base_url='<?php echo base_url();?>'; $(function(){ delete var t='3000';if(isNullOrEmpty(time)){t='3000';}else{t=time;} var icon='<?php echo base_url();?>assets/dialog/icons/'; showDialog('确定要删除?',function(){ var url='<?=@$site_url.'/'.@$module.'/delete';?>'; var data={'Id':id} $.ajax({ //async: true,//是否为异步请求 type: "POST",//GET POST url: url, //data: data, data:'Id='+id, //dataType: "json", //beforeSend: function(XMLHttpRequest, textStatus){}, success: function(data){//,textStatus //console.log(data); var msg=data.message; //if(data.status){refreshGrid();i='succeed.png';}else{i='info.png';} showDialog(msg); window.location.reload(); } //complete: function(XMLHttpRequest, textStatus){}, /*error: function(XMLHttpRequest, textStatus, errorThrown){ var msg=("Error");i='error.png'; showDialog(msg); }*/ }); }); } }); </script>
public function delete(){ if (! isPost ()) { $msg='输入的链接不正确!'; showErrorMsg($msg); } $action='delete'; $module=$this->module; $arrLang=lang('common_'.$module); $title=$arrLang[$action]; $arr_post=$this->input->post(); $arr_post=setForm($arr_post); $Id=@$arr_post['Id']; $status=FALSE; $logType=0; if(!isNullOrEmpty($Id)){ $del_where=array('ID'=>$Id); $this->common_model->table='mx_changci'; $result = $this->common_model->delete( $del_where ); if ($result) { $status = TRUE; $message = $title.'成功!'; } else { $status = FALSE; $message = $title.'失败!'; } }else{ $status=FALSE; $message= $title.'出错!'; $logType=3; } $jsonData['status']=$status; $jsonData['message']=$message; header("Content-type: application/json"); echo json_encode($jsonData); }
The above introduces Xin Xing's brief analysis of the implementation process of ajax, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.