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='+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; } ?>
この記事では、ajaxとmysqlデータベース、smartyを使用して、部分データ状態の更新方法を紹介しました。これで全ての内容を共有しました。皆様に参考になれば幸いです。また、ナイアラチュートリアルを多く応援していただけると嬉しいです。
声明:この記事の内容はインターネットから取得しており、著作権者に帰属します。インターネットユーザーが自発的に貢献し、自己でアップロードしたものであり、このサイトは所有権を持ちません。また、人工的な編集は行われていません。著作権侵害を疑う内容があれば、notice#wまでメールを送ってください。3codebox.com(メールを送信する際、#を@に変更してください)で通報し、関連する証拠を提供してください。確認がとれた場合、このサイトは即座に侵害を疑われるコンテンツを削除します。