Home > Backend Development > PHP Tutorial > Xin Xing briefly analyzes the implementation process of ajax

Xin Xing briefly analyzes the implementation process of ajax

WBOY
Release: 2016-08-08 09:22:59
Original
1094 people have browsed it

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>';
Copy after login

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.&#39;/&#39;.@$module.&#39;/delete&#39;;?>';
				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>
Copy after login

At this point, we are one step away from completion, which is the delete method. Here is its implementation process:

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);
	}
Copy after login

This is a simple ajax implementation in a project I recently participated in

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template