json-php-mysql-jquery-ajax how can i update without refresh -


i have following example.


userview.php

<script>         $(document).on("ready", function(){             loaddata();         });           var loaddata = function(){             $.ajax({                 type:"post",                 url:"users.php"             }).done(function(data){                 console.log(data);                 var users = json.parse(data);                 for(var in users){                     $("#content").append(users[ ].usuario + " " + users[ ].nombres + "<br>");                 }             });         }      </script> 

users.php

  <?php     $bd = "test";     $server ="localhost";     $user = "root";     $password = "";      $conexion = @mysqli_connect($server, $user, $password, $bd);      if( ! $conexion )   die( "error de conexion ".mysqli_connect_error() );      $sql = "select usuario, nombres usuario";     $result = mysqli_query($conexion, $sql);     $array_user = array();     while($data = mysqli_fetch_assoc($result)){         $array_user[] = $data;     }      echo json_encode($array_user); ?> 

how can reload users (in case of update or delete) in userview.php without refreshing page

a solution making loaddata() run, lets say, every 5 seconds, i.e.:

<script> $(document).ready(function() {     startloop(); });  var frequency = 5000; // 5 seconds in miliseconds var interval = 0; // starts , resets loop function startloop() {     if (interval > 0) clearinterval(interval); // stop     interval = setinterval("loaddata()", frequency); // run }  function loaddata() {     $.ajax({         type: "post",         url: "users.php"     }).done(function(data) {         console.log(data);         var users = json.parse(data);         $("#content").empty();         (var in users) {             $("#content").append(users[i].usuario + " " + users[i].nombres + "<br>");         }     }); } </script> 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -