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>/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><th>役割</th>/th><th>操作</th>/th>
      </tr>  
      {foreach $lists as $list}
        <tr align="center">
          <td>{$list.uid}</td>/td>
          <td>{$list.username}</td>/td>
          <td>{$list.password}</td>/td>
          {if $list.is_lock==1}
            <td><a href="javascript:lock(0,{$list.uid});" rel="external nofollow">ロック</a>/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(メール送信時、#を@に変更して報告してください。関連する証拠を提供し、一旦確認ができ次第、このサイトは侵害された内容をすぐに削除します。)

おすすめ