search
Homephp教程php手册ThinkPHP中ajax使用实例教程,thinkphpajax

ThinkPHP中ajax使用实例教程,thinkphpajax

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

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

一、jquery实现方法:

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

<&#63;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);  
    }
  }
 
}
&#63;>

模板index.html代码如下:

<html>
<head>
<script type="text/javascript" src="__PUBLIC__/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
  $(function(){
    $('input:button').click(function(){
      var $title=$('input[name="title"]').val();
      var $message=$('input[name="message"]').val();
      $mess=$('#mess');
      $.getJSON('__URL__/add',{title:$title,message:$message},function(json){
        //alert(json);return false;
        if(json.status==1){
          $mess.slideDown(3000,function(){
            $mess.css('display','block');  
          }).html('标题为'+json.data.title+'信息为'+json.data.message);  
        }else{
          $mess.slideDown(3000,function(){
            $mess.css('display','block');  
          }).html('信息添加失败,请检查');  
        }    
      });
    })  
  })
</script>
</head>
<body>
<div style="display:none; color:red;" id="mess"></div>
<form action="" method="get">
 标题:<input type="text" name="title" /><br />
 信息:<input type="text" name="message" /><br />
    <input type="button" value="提交" />
</form>
</body>
</html>

二、ThinkPHP实现方法:

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

<&#63;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());  
    }
  }
}
&#63;>

模板index.html代码如下:

<html>
<head>
<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/ThinkAjax.js"></script>
<script type="text/javascript">
  function add(){
    //ThinkAjax.sendForm(表单ID,URL,回调函数,信息显示的地方);
    ThinkAjax.sendForm('frm','__URL__/addtwo',wc);  
  }
  function wc(data,status){
    if(status!=1){
      alert('发送失败');
    }else{
      $('list').innerHTML+='标题'+data.title+',信息'+data.message;  
    }  
  }
</script>

</head>
<body>
<div id="list"></div>
<form action="" method="POST" id="frm">
 标题:<input type="text" name="title" /><br />
 信息:<input type="text" name="message" /><br />
    <input type="button" value="提交" onClick="add()" />
</form>
</body>
</html>

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

think php 怎做ajax交互 有有简单易懂的例子 不胜感激啦

thinkphp自带有ajax方法,很简单的使用,方便的交互。举一个例子你就明白啦。
先包含所需要的js(他的官网里有下载),路径自己修改。

你那个回调函数里的 参数写错
这样写
alert(dd.data);

你没了解 ajaxReturn返回的数据格式
你可以直接通过 浏览器访问 is_user 就可以看到
 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)