English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

ajax를 사용하여 상태를 변경하고 무�新刷新의 예제

1. 01.php이 주 프로그램으로, smarty 템플릿을 호출하여 표준화된 출력을 생성합니다:

<?php
  include './include/Mysql.class.php';
  include './libs/Smarty.class.php';
  $db=new Mysql;
  $smarty=new Smarty;
  $lists=$db->getALL('users');
  $smarty->assign('lists',$lists);
  $smarty->display('list.html');
?>

2. list.html 템플릿: 내용이 JS ajax와 함께 사용됩니다:

<!DOCTYPE html>
<html>
<head>
  <meta charset=utf-8>
  <title>사용자 권한 표시 테이블</title>
</head>
<body>
    //table의 body에 div를 설정하여 js 호출을 쉽게 합니다
    <div id="table">
    <table align="center" border="1" width="500">
      <center><h2>사용자 권한 테이블</h2></center>
      <tr>
        <th>uid</th><th>사용자 이름</th><th>암호</th><th>봉인 상태</th><th>역할</th><th>작업</th>
      </tr>  
      {foreach $lists as $list}
        <tr align="center">
          <td>{$list.uid}</td>
          <td>{$list.username}</td>
          <td>{$list.password}</td>
          {if $list.is_lock==1}
            <td><a href="javascript:lock(0,{$list.uid});" rel="external nofollow" >봉인</a></td>
            {else}
            <td><a href="javascript:lock(1,{$list.uid})" rel="external nofollow" ;>해제 잠금</a></td>  
          {/if}    
          {if $list.role==1}
              <td>관리자</td>
          {else}
              <td>편집자</td>    
          {/if}
          <td><a href="javascript:del({$list.uid})" rel="external nofollow" >삭제</a></td>
        </tr>    
      {/foreach}  
    </table>
    </div>  
</body>
    <script type="text/javascript">
      function lock(lock,uid){
          //ajax 객체를 생성합니다
          var xhr=new XMLHttpRequest();
          //링크를 엽니다
          xhr.open('post','02.php');
          //헤더 정보 설정
          xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
          //값, 여러 개의 매개변수는 '&'로 분리됩니다
          var data="is_lock="+lock+"&uid="+uid;
          //ajax 데이터 요청을 보냅니다
          xhr.send(data);
          //回调 설정, 감지 함수 설정
          xhr.onreadystatechange=function(){
            //ajax 상태 코드가 정상적으로 응답하면 그리고 네트워크가 정상적이면, 응답 텍스트를 가져옵니다
            if(xhr.readyState==4&&xhr.status==200){
              if(xhr.responseText){
                document.getElementById('table').innerHTML=xhr.responseText;
              }else{
                alert("상태 전환 실패!");
              }
            }
          }
        }
    function del(uid){
      var del=window.confirm("삭제하시겠습니까?");
      if(del){
        //ajax 객체를 생성합니다
        var xhr=new XMLHttpRequest();
        //링크를 엽니다
        xhr.open('post','del.php');
        //header 헤더 설정
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
        //data 값
        var data="uid="+uid;
        //ajax 요청을 보냅니다
        xhr.send(data);
        //감지 설정
        xhr.onreadystatechange=function(){
          //ajax 상태 코드가 정상적으로 응답하면 그리고 네트워크가 정상적이면, 응답 텍스트를 가져옵니다
          if(xhr.readyState==4&&xhr.status==200){
            if(xhr.responseText){
              //ajax 응답 내용을 이 템플릿의 table 태그 내용으로 대체합니다
              document.getElementById('table').innerHTML=xhr.responseText;
            }else{
              alert("삭제 실패!");}}
            }
          }
        }
      }
    }    
    </script>
</html>

3. 02.php 상태 무�新고 없는 변경:

<?php
  include './include/Mysql.class.php';
  include './libs/Smarty.class.php';
  $lock=$_POST['is_lock'];
  $uid=$_POST['uid'];
  $smarty=new Smarty;
  $db=new Mysql;
  $result=$db->update('users',"is_lock=$lock","uid=$uid");
  if($result){
    //성공적으로 수정되면 데이터베이스를 다시 탐색하여 Smarty 템플릿을 출력합니다
    $lists=$db->getALL('users');
    $smarty->assign('lists',$lists);
    $smarty->display('list.html');
  }else{
    echo false;
  }
?>

4.del.php 무�新고 없는 제거 구현

<?php
  include './include/Mysql.class.php';
  include './libs/Smarty.class.php';
  $db=new Mysql;
  $smarty=new Smarty;
  $uid=$_POST['uid'];
  $res=$db->delete('users',$uid);
  if($res>0){
    $lists=$db->getALL('users');
    $smarty->assign('lists',$lists);
    $smarty->display('list.html');
  }else{
    echo false;
  }
?>

이상의 AJAX 상태 변경 및 무�新고 없는 제거 예제가 저가 공유한 모든 내용입니다. 이를 통해 참고를 제공하고, 모두가 힘을 합쳐 강의를 지지해 주기를 바랍니다.

선언:이 문서의 내용은 인터넷에서 가져왔으며, 저작권자가 모두 소유하고 있습니다. 내용은 인터넷 사용자가 자발적으로 기여하고 자체로 업로드한 것이며,이 사이트는 소유권을 가지지 않으며 인공 편집 처리를 하지 않으며, 관련 법적 책임을 부담하지 않습니다. 저작권 위반된 내용이 있을 경우, notice#w로 이메일을 보내 주세요.3codebox.com(이메일을 보내면, #을 @으로 변경하십시오. 신고하고 관련 증거를 제공하십시오. 실제로 확인되면,이 사이트는 즉시 위반된 내용을 삭제합니다.)

좋아하는 것