The example in this article describes the method of using Ajax in thinkphp, and submits the form as shown in the following illustration:
Click Submit, do not need to refresh this page, submit content to the database, and display the content submitted on this page. As shown in the following illustration:
First, jquery implementation method:
The MessageAction.class.php page code is as follows:
<?php
class Messageaction extends action{
function index () {
$this->display ();
}
function Add () {
//ajaxreturn (data, ' hint ', state)
$m =m (' message ');
if ($m->add ($_get)) {
$this->ajaxreturn ($_get, ' Add information succeeded ', 1);
} else{
$this->ajaxreturn (0, ' Add info failed ', 0);
}
}
? >
The template index.html code is as follows:
Second, thinkphp implementation methods:
The MessageAction.class.php page code is as follows:
<?php
class Messageaction extends action{
function index () {
$this->display ();
}
function Addtwo () {
$m =m (' message ');
if ($vo = $m->create ()) {
if ($m->add ()) {
$this->ajaxreturn ($vo, ' Add success ', 1);
} else{
$this->ajaxreturn (0, ' Add failed ', 0);
}
else{
$this->error ($m->geterror ());}}
? >
The template index.html code is as follows:
Interested friends can test and run the examples shown in this article can deepen the understanding of AJAX applications.
More interested in thinkphp related content readers can view the site topics: "thinkphp Introductory Course", "thinkphp Template Operation Skills Summary", "thinkphp Common Methods Summary", "Smarty Template Introductory Course" and "PHP template technology Summary."
I hope this article will help you with the PHP program design based on thinkphp framework.