samedi 18 avril 2015

How to load newer data from database using Ajax and PHP [on hold]

Just like Facebook, it will load automatically the new comments or posts.

I guess I will be using the setInterval function and inside of it will be the ajax code.



// my index.php

<script>
// I dont know if my ajax is correct
$(function(){
setInterval(ajaxCall, 2000);

function ajaxCall() {
$.ajax({
type: "POST",
url: "latest.php",
dataType: 'JSON',
success: function(data)
{
// dont know what to do here

}

});
}
});
</script>

<body>

<div id="latest">
<!-- latest news from db here -->
</div>

</body>


This is my latest.php I don'w know if it is correct. I already tested it but it won't works.



<?php
include ('dbconnection.php');

$get = "SELECT * FROM news";
$comments = array();

$res = $conect->query($get);
while($row=mysqli_fetch_assoc($res)) // loop through news
{
// add each one to the comment container
$comments[] = array($row['title'], $row['news']);
}


$comments = json_encode($comments);

die($comments);
?>

Aucun commentaire:

Enregistrer un commentaire