作者:mobiledu2502928043 | 来源:互联网 | 2014-06-12 18:37
点击提交,不需要刷新本页,将内容提交到数据库当中,并在本页显示提交的内容.jquery方法:MessageAction.class.php<?phpclassMessageActionext...
点击提交,不需要刷新本页,将内容提交到数据库当中,并在本页显示提交的内容.
jquery方法:
MessageAction.class.php
- class MessageAction extends Action{
-
- function index(){
- $this->display();
- }
-
- function add(){
-
- $m=M(&#39;message&#39;);
- if($m->add($_GET)){
- $this->ajaxReturn($_GET,&#39;添加信息成功&#39;,1);
- }else{
- $this->ajaxReturn(0,&#39;添加信息失败&#39;,0);
- }
- }
-
- }
- ?>
模板index.html
- <html>
- <head>
- <script type="text/Javascript" src="__PUBLIC__/js/jquery-1.7.1.min.js">script>
- <script type="text/Javascript">
- $(function(){
- $(&#39;input:button&#39;).click(function(){
- var $title=$(&#39;input[name="title"]&#39;).val();
- var $message=$(&#39;input[name="message"]&#39;).val();
- $mess=$(&#39;#mess&#39;);
- $.getJSON(&#39;__URL__/add&#39;,{title:$title,message:$message},function(json){
- //alert(json);return false;
- if(json.status==1){
- $mess.slideDown(3000,function(){
- $mess.css(&#39;display&#39;,&#39;block&#39;);
- }).html(&#39;标题为&#39;+json.data.title+&#39;信息为&#39;+json.data.message);
- }else{
- $mess.slideDown(3000,function(){
- $mess.css(&#39;display&#39;,&#39;block&#39;);
- }).html(&#39;信息添加失败,请检查&#39;);
- }
- });
- })
- })
- 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
- class MessageAction extends Action{
-
- function index(){
- $this->display();
- }
-
- function addtwo(){
- $m=M(&#39;message&#39;);
- if($vo=$m->create()){
- if($m->add()){
- $this->ajaxReturn($vo,&#39;添加成功&#39;,1);
- }else{
- $this->ajaxReturn(0,&#39;添加失败&#39;,0);
- }
- }else{
- $this->error($m->getError());
- }
-
- }
-
- }
- ?>
模板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(&#39;frm&#39;,&#39;__URL__/addtwo&#39;,wc);
- }
- function wc(data,status){
- if(status!=1){
- alert(&#39;发送失败&#39;);
- }else{
- $(&#39;list&#39;).innerHTML+=&#39;标题&#39;+data.title+&#39;,信息&#39;+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>