English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
상태상태: 클릭하여 상태잠금 및 해제 간의 전환을 실현
1.주프로그램:01.php를 통해 smarty와 mysql 클래스를 가져오고 데이터를 목록 템플릿에 가져옵니다
<?} 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.리스트 템플릿은 smarty를 사용하여 템플릿 데이터를 순회하며 표시하며, ajax를 통해 잠금 상태를 변경합니다
!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>사용자 권한 표시 테이블</title> </head> <body> <table align="center" border="1" width="500"> <center><h2>사용자 권한 테이블</h2></center> <tr> <th>uid</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} </tr> {/foreach} </table> </body> <script type="text/javascript"> function lock(lock,uid){ //ajax 객체를 생성합니다 var xhr=new XMLHttpRequest(); //한 링크를 엽니다 xhr.open('get','02.php?is_lock='+락+"&uid=")}}+uid); //ajax 요청 전송 xhr.send(null); //콜백, 리스너 함수 설정 xhr.onreadystatechange=function(){ //ajax 상태 코드가 정상적으로 응답하면 및 네트워크가 정상적이면, 응답 텍스트를 가져옵니다 if(xhr.readyState==4&&xhr.status==200){ if(xhr.responseText){ window.location.reload(); } alert("상태 전환 실패!"); } } } } </script> </html>
3.ajax 응답 스크립트는 ajax가 get 방식으로 전달된 데이터를 받아 데이터베이스 내용을 변경하고 텍스트를 반환하여 js 스크립트에 응답합니다
<?} include './include/Mysql.class.php'; $lock=$_GET['is_lock']; $uid=$_GET['uid']; $db=new Mysql; $result=$db->update('users',"is_lock=$lock","uid=$uid"); if($result){ echo true; } echo false; } ?>
이제 mysql 데이터베이스와 smarty를 사용하여 일부 데이터 상태를 갱신하는 방법을 소개해 드린 것입니다. 이를 통해 참고할 수 있기를 바라며,呐喊 교육에 많은 지지를 부탁드립니다.
고지사항: 본문은 인터넷에서 가져온 것이며, 저작권자는 본 사이트에 소유되어 있지 않으며, 인터넷 사용자가 자발적으로 기여하고 자체로 업로드한 내용으로, 본 사이트는 소유권을 가지지 않으며, 인공적으로 편집되지 않았으며, 관련 법적 책임을 부담하지 않습니다. 저작권 침해가 의심되는 내용이 있으시면, notice#w로 이메일을 보내 주시기 바랍니다.3codebox.com에 (보내는 이메일에서 #을 @으로 변경하십시오) 신고하시고 관련 증거를 제공하시면, 사실이 확인되면 해당 사이트는 즉시 저작권 침해 내용을 삭제합니다.