Welcome Guest, Not a member yet? Register   Sign In
How to reload the div using ajax
#1

[eluser]JasmineFlower[/eluser]
hi,

I need to reload the below div within the time interval ,how to do this in ajax

the below code displays the folder files in html page.


Code:
<div id="ajaxusing">
&lt;?php  

$dir="../skycad/fence/Fence01";
if(is_dir($dir))
{
if ($handle = opendir($dir))
{
  while (false !== ($file = readdir($handle)))
  {  
     if ($file != "." && $file != "..")
     {  
       echo "<ahref= \" ../skycad/fence/popup.php?filename=$file \"  >". $file." </a>";        
     }  
  }
  closedir($handle);  
}
}
?&gt;
</div>
#2

[eluser]eoinmcg[/eluser]
firstly, refactor your code and embrace MVC.

i'd move the building of the file list to a model.
in your controller you'll then get the files from the model and send it to a view.

Code:
//in your controller
$dir = 'path/to/dir';
$data['files'] = $this->model_name->build_file_list($dir);
$this->load->view('viewname.php', $data);


//in your view: viewname.php
<div id="ajax_wrap">
<div id="ajax_using">
&lt;?php foreach ($files as $file): ?&gt;
<a href="&lt;?php echo ">&lt;?php echo $file; ?&gt;</a>
&lt;?php endforeach; ?&gt;
</div>
</div>

now that things are not such a mess, a splash of jquery would be the easiest way to update
Code:
update();

function update() {
  $.ajax({
    type: 'POST',
    url: '/controller/method',
    timeout: 2000,
    success: function(data) {
        $("#ajax_wrap").html(data);

      window.setTimeout(update, 20000);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        // deal with possible errors
      window.setTimeout(update, 20000);
    }
    });
}




Theme © iAndrew 2016 - Forum software by © MyBB