Categories > TinyButStrong general >

tbs and simple Ajax

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: thuc101
Date: 2010-03-27
Time: 04:59

tbs and simple Ajax

I have code use Ajax like this
test.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
loadstatustext = 'Loading...';
function ajaxLoad(url,id){
       if (document.getElementById) {
           var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
       }
       if (x){
           x.onreadystatechange = function(){
                       el = document.getElementById(id);
                       el.innerHTML = loadstatustext;
                           if (x.readyState == 4 && x.status == 200){
                               el.innerHTML = x.responseText;
                           }
           }
           x.open("GET", url, true);
           x.send(null);
        }
}
function on_key_up(x){
    ajaxLoad('<?=$mosConfig_live_site;?>/com/testchat/noidung.php?txt='+document.getElementById('txt').value,'hienthi');
}
function on_click(x){
    ajaxLoad('answer.php?w='+x,'hienthi');
}
</script>
</head>
<body>
<a href="javascript:void(0);" onClick="javascript:on_click('tb1');">view table 1</a> |
<a href="javascript:void(0);" onClick="javascript:on_click('tb2');">view table 2</a> |
<a href="javascript:void(0);" onClick="javascript:on_click('tb3');">view table 3</a> |
<hr>
<div id='hienthi'>...</div>
</body>
</html>
answer.php
<?php
if(isset($_GET['w'])){
    $w=$_GET['w'];
}else $w='';
    switch($w){
        case 'tb1':
            printf('This is table 1');
            $tb1[]=array('id'=>0,'name'=>'one');
            $tb1[]=array('id'=>1,'name'=>'two');
            $tb1[]=array('id'=>2,'name'=>'three');
            printf('%s','<table border=1>');
            foreach($tb1 as $node){
                printf('<tr><td>%d</td><td>%s</td></tr>',$node['id'],$node['name']);
            }
            printf('%s','</table>');
            //How to Merge by tbs in this case, help me, thank you!!!
            break;
        case 'tb2':
            printf('this is table 2');
            break;           
        case 'tb3':
            printf('this is table 3');
            break;           
        default:
            break;
    }
?>
How I can use tbs in file answer.php, help me, thank a lot!!!
By: Skrol29
Date: 2010-03-27
Time: 12:40

Re: tbs and simple Ajax

rapid reply:

<?php
if(isset($_GET['w'])){
$w=$_GET['w'];
}else $w='';
switch($w){
  case 'tb1':
   $tb1[]=array('id'=>1,'name'=>'two');
   $tb1[]=array('id'=>2,'name'=>'three');
   include_once('tbs_calss.php');
   $TBS = new clsTinyButStrong;
   $TBS->Source = 'This is table 1<table border=1><tr><td>[n.id;block=tr]</td><td>[n.name]</td></tr></table>';
   $TBS->MergeBlock('n', $tb1);
   $TBS->Show(); // merge, echo and exit the script
   break;
  case 'tb2':
   printf('this is table 2');
   break; 
  case 'tb3':
   printf('this is table 3');
   break; 
  default:
   break;
}
?>

Of course you can use a $TBS->LoadTemplate() instead of a a $TBS->Source = '...'
And your template can contain conditional sections in order to replace the "switch $w" or the PHP part.
By: thuc101
Date: 2010-03-27
Time: 13:47

Re: tbs and simple Ajax

oh, thank you very much!!!
tbs is super!!