当前位置:Gxlcms > PHP教程 > ThinkPHP中ajax使用实例教程_php实例

ThinkPHP中ajax使用实例教程_php实例

时间:2021-07-01 10:21:17 帮助过:30人阅读

本文实例讲述了ThinkPHP中使用ajax的方法,提交表单如下图所示:

点击提交,不需要刷新本页,将内容提交到数据库当中,并在本页显示提交的内容。如下图所示:

一、jquery实现方法:

MessageAction.class.php页面代码如下:

<?php
class MessageAction extends Action{
  
  function index(){
    $this->display();  
  }
  
  function add(){
    //ajaxReturn(数据,'提示信息',状态)  
    $m=M('message');
    if($m->add($_GET)){
      $this->ajaxReturn($_GET,'添加信息成功',1);
    }else{
      $this->ajaxReturn(0,'添加信息失败',0);  
    }
  }
 
}
?>

模板index.html代码如下:












二、ThinkPHP实现方法:

MessageAction.class.php页面代码如下:

<?php
class MessageAction extends Action{
  
  function index(){
    $this->display();  
  }

  function addtwo(){
    $m=M('message');
    if($vo=$m->create()){
      if($m->add()){
        $this->ajaxReturn($vo,'添加成功',1);  
      }else{
        $this->ajaxReturn(0,'添加失败',0);  
      }  
    }else{
      $this->error($m->getError());  
    }
  }
}
?>

模板index.html代码如下:
















感兴趣的朋友可以测试运行一下本文所示实例,可以加深对Ajax应用的理解。

人气教程排行