thinkphp Ajax Use Instance tutorial _php instance

Source: Internet
Author: User
Tags php template smarty template

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.