Preparations:
① The ThinkPHP framework should be used first
② It is better to have some ajax BASICS (you can go to another blog post of Xiaofei: Ajax real-time verification of "username/email" to see if it already exists)
③ 4 js documents (Click here to download without points)
First paste the source code:
Copy codeThe Code is as follows:
<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:
Adds an onclick event for the "check" button. 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 ){......}
We can see that the ThinkAjax. send method has six parameters:
Parameter url: indicates the method on which the data transmitted from the client browser is submitted to the server for processing. Here I submit it to the "checktitle method under the current module" for processing.
Pars: this parameter is equivalent to the string parameter in the send method in ajax. It indicates the data to be submitted in the past. This parameter is only used to pass values in post mode.
Response: Custom callback function. If a callback function is defined, the server submits the processed data to the callback function after processing the submitted data. The callback function has two parameters: ① data ② status parameter data: the data processed by the server is assigned to the data parameter status: indicating the status information after processing, 1 indicating success 0 indicating failure
Target: indicates the location where the processed data is displayed (or output). For example, if this parameter is set to checkbox, indicates that the processed data will be output in the id = "checkbox" label.
Source code of the checktitle method in the current module:
Copy codeThe Code is as follows:
<? Php
Class IndexAction extends Action
{
// Homepage
Public function index (){
$ This-> display ();
}
// Check whether the title is available
Public function checkTitle ()
{
If (! Empty ($ _ POST ['title'])
{
$ Form = D ("Form ");
If ($ Form-> getByTitle ($ _ POST ['title'])
{
$ This-> error ('title already exists ');
}
Else
{
$ This-> success ('title can be used! ');
}
}
Else
{
$ This-> error ('title cannot be blank ...');
}
}
}
?>
Author: WEB development _ Xiaofei