ÎÞˢзÖÒ³¼¼Êõ£¬Ajax+smarty_Php_ÍøÕ¾µ¼º½ - ¿ÆÄɿƼ¼
µ±Ç°Î»ÖãºÊ×Ò³ > Source code¡¬Ô´Âë ½âÃÜÃÜÂ룺uoslin.com > Php > ÏêϸÄÚÈÝ
ÎÞˢзÖÒ³¼¼Êõ£¬Ajax+smarty
·¢²¼Ê±¼ä£º2016/3/24  ÔĶÁ´ÎÊý£º488  ×ÖÌå´óС: ¡¾Ð¡¡¿ ¡¾ÖС¿¡¾´ó¡¿
ÕâÀïÔËÓÃSmartyÄ£°å£¬¸ü¼òµ¥

±¾ÎÄÖ÷ÒªµÄ¼¼Êõ£ºAJAX£¬PHP£¬Smarty,ÁíÍâ×Ô¼º·â×°ÁËÒ»¸öºÜ¼òµ¥µÄÀà

Àࣺ

(function(){
    function $(id) {
        return document.getElementById(id);
    }
    $.init=function() {
        try{return new XMLHttpRequest();}catch(e){};
        try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}
        alert('Çë¸ü»»ä¯ÀÀÆ÷');
    }
    $.get=function (url,data,callback,type) {
        var xhr = this.init();
        url += '?' +new Date().getTime();
        if(data!=null){
            url += '&'+data;
        }
        xhr.open('get',url);
        xhr.onreadystatechange = function () {
            if(xhr.readyState == 4 && xhr.status == 200){
                if(type==null){
                    callback(xhr.responseText);
                }
                if(type == 'text'){
                    callback(xhr.responseText);
                }
                if(type == 'xml'){
                    callback(xhr.responseXML);
                }
                if(type == 'json'){
                    callback(eval("("+xhr.responseText+")"));
                }
            }
        }
        xhr.send(null);
    }

$.post = function (url,data,callback,type) {
        var xhr = this.init();
        xhr.open('post',url);
        xhr.setRequestHeader('Content-Type','Application/x-www-form-urlencoded');
        xhr.onreadystatechange = function () {
            if(xhr.readyState == 4 && xhr.status == 200){
                if(type==null){
                    callback(xhr.responseText);
                }
                if(type == 'text'){
                    callback(xhr.responseText);
                }
                if(type == 'xml'){
                    callback(xhr.responseXML);
                }
                if(type == 'json'){
                    callback(eval("("+xhr.responseText+")"));
                }
            }
        }; 
        xhr.send(data);
    }

ÀàºÜ¼òµ¥£¬²»½éÉÜÁË

HTML:

window.onload=function () {
        init(2);
    }
    function del(id,p){
        $.get('del.php','id='+id,function(msg){
                if(msg==1){
                    init(p);
                }
        })
    }
    function init(p) {
        $.get('page.php','page='+p,function(msg){
            $('result').innerHTML=msg;
        });
    }

 

PHP£º

<?php
header('content-type:text/html;charset=utf-8');
mysql_connect('localhost','root','root');
mysql_select_db('db2');
mysql_query('set names utf8');
$re=mysql_query('select count(*) as num from catgory');
$hang=mysql_fetch_assoc($re);
$row1=$hang['num'];
$page=isset($_GET['page'])? $_GET['page']:1;
$pagesize=2;
$total=ceil($row1/$pagesize);
$prev=$page-1;
$next=$page+1;
if($prev<1){$prev=1;}
if($next>$total){$next=$total;}
$offset = ($page-1)*$pagesize;
$result=mysql_query("select * from catgory limit ".$offset.','.$pagesize);
$ct=mysql_num_rows($result);
$data=array();
for($i=0;$i<$ct;$i++){
    $r=mysql_fetch_assoc($result);
    $data[]=$r;
}
include('libs/Smarty.class.php');
$Smarty=new Smarty();
$Smarty->assign('data',$data);
$Smarty->assign('page',$page);
$Smarty->assign('pagesize',$pagesize);
$Smarty->assign('total',$total);
$Smarty->assign('prev',$prev);
$Smarty->assign('next',$next);
$Smarty->assign('row1',$row1);
$str=$Smarty->fetch('page.html');
echo $str;

Smarty:

   <table border="1" rules="all" width="600">
        <tr>
            <td>±àºÅ</td>
            <td>Æ·ÅÆ</td>
            <td>pid</td>
            <td>ɾ³ý</td>
        </tr>
        {foreach from=$data item='value'}
        <tr>
            <td>{$value['id']}</td>
            <td>{$value['name']}</td>
            <td>{$value['pid']}</td>
            <td><a href="#", onclick="del({$value['id']},{$page})">ɾ³ý</td>
        </tr>
        {/foreach}
        <tr>
            <td colspan="3">
                ¹²{$total}Ò³
                µÚ{$page}Ò³
                <a href="#", onclick="init({$prev})">ÉÏÒ»Ò³</a>
                <a href="#", onclick="init({$next})">ÏÂÒ»Ò³</a>
                <a href="#", onclick="init(1)">µÚÒ»Ò³</a>
                <a href="#", onclick="init({$total})">×îĩҳ</a>
                ¹²{$row1}ÌõÊý¾Ý
                ÿҳ{$pagesize}ÌõÊý¾Ý
            </td>
        </tr>
    </table>

ÒªÒýÈësmaryÄ£°å

Õâ¾ÍʵÏÖÁËÒ»¸ö¼òµ¥µÄ·ÖÒ³¼¼Êõ
ÉÏһƪ£ºÃ»ÓÐÁË£¡ ÏÂһƪ£ºWeb¶ËPHP´úÂ뺯Êý¸²¸ÇÂʲâÊÔ½â¾ö·½°¸
ÎÒÒªÆÀÂÛ
  • ÄäÃû·¢±í
  • [Ìí¼Óµ½ÊղؼÐ]
  • ·¢±íÆÀÂÛ£º(ÄäÃû·¢±íÎÞÐèµÇ¼£¬ÒѵǼÓû§¿ÉÖ±½Ó·¢±í¡£) µÇ¼״̬£ºÎ´µÇ¼
×îÐÂÆÀÂÛ
ËùÓÐÆÀÂÛ[0]
    ÔÝÎÞÒÑÉóºËÆÀÂÛ!


Copyright © 2012 ,Inc. All rights reserved