Using ThinkPHP built in ThinkAjax asynchronous transmission technology implementation method

  • 2020-05-10 17:52:56
  • OfStack

Preparation:
First of all, you should be able to use the ThinkPHP framework
It is better to have some basis of ajax (you can go to another blog post of xiaofei: Ajax to verify the existence of "user name/email, etc." in real time)
4 js documents (click here to download free credits)

First paste the source code:
 
<script type="text/javascript" src="__PUBLIC__/js/base.js"></script> 
<script type="text/javascript" src="__PUBLIC__/js/prototype.js"></script> 
<script type="text/javascript" src="__PUBLIC__/js/mootools.js"></script> 
<script type="text/javascript" src="__PUBLIC__/js/Ajax/ThinkAjax.js"></script> 
<script type="text/javascript"> 
function checktitle() 
{ 
ThinkAjax.send('__URL__/checktitle','ajax=1&title='+$('title').value,'','checkbox'); 
} 
</script> 
<form action="__URL__/insert" method="post" id="myform"> 
<table> 
<tbody> 
<tr> 
<td width="45" class="tRight"> Title: </td> 
<td> 
<input type="text" id="title" name="title"> 
<input type="button" value=" check " onClick="checktitle();"> 
</td> 
<td> 
<span id="checkbox"></span> 
</td> 
</tr> 
</tbody> 
</table> 
</form> 

Code explanation:
Add an onclick event to the check button, and when the button is clicked, the checktitle() function is called
In the checktitle function, we only use the member method send in the ThinkAjax object
send: function (url, pars response, target, tips, effect) {... }
It can be seen that the ThinkAjax.send method has six parameters:
Parameter url: represents which method to submit the data transferred from the client browser to the server for processing. Here I submit it to "checktitle method under the current module" for processing
Parameter pars: equivalent to the string parameter in the send method in ajax, which represents the past data to be submitted. This parameter is only used for post value transfer
Parameter response: custom callback function. If the callback function is defined, the server will pass the processed data to the callback function for processing after it has processed the submitted data. This callback function has two parameters: data status parameter data: assign the data processed by the server side to data parameter status: represent the status information after processing, and 1 represents success and 0 represents failure
Parameter target: indicates where the processed data will be displayed (or output). For example, if I assign this parameter to: checkbox, the processed data will be output in the label id= "checkbox"
The source code of checktitle method under the current module:
 
<?php 
class IndexAction extends Action 
{ 
//  Home page  
public function index(){ 
$this->display(); 
} 
//  Check if the title is available  
public function checkTitle() 
{ 
if(!empty($_POST['title'])) 
{ 
$Form = D("Form"); 
if($Form->getByTitle($_POST['title'])) 
{ 
$this->error(' The title already exists '); 
} 
else 
{ 
$this->success(' The title can be used !'); 
} 
} 
else 
{ 
$this->error(' The title cannot be empty ...'); 
} 
} 
} 
?> 

Article by: WEB developer _ xiaofei

Related articles: