Tony Tony - 6 months ago 18
MySQL Question

Mysql with ajax

Here is my code that I'm using.

ajax

$.ajax({
type: "GET",
url: "process.php",
dataType: "html",
success: function(data){
$(".content").html(data);
}
});

$("#submit").click(function(){
$.ajax({
type: "POST",
url: "post.php",
dataType: "html",
success: function(data1){
$(".content").html(data1);
}
});
});


process.php

<?php
$db = mysqli_connect("localhost","root","","webcap");
$query = mysqli_query($db,"SELECT * FROM test");
while($row = mysqli_fetch_assoc($query)){
echo $row['num'];
}


post.php

<?php
$db = mysqli_connect("localhost","root","","webcap");
$num_query = mysqli_query($db,"SELECT * FROM test");
$num_row = mysqli_fetch_assoc($num_query);
$number = $num_row['num'];

$new_number = $number + 1;
$query2 = mysqli_query($db,"UPDATE test SET num='$new_number'");
echo $new_number;


The idea is when i click button it add plus one to my database and i can see it without refreshing but others don't.

I want that everyone in my website see it in real time without refreshing

Answer

If your ajax is only called in the javascript once, it won't update without refreshing. You need to put the ajax into setInterval and pick a refresh rate (ms):

Interval = setInterval(function () {
$.ajax({
    type: "GET",
    url: "process.php",
    dataType: "html",
    success: function(data){
        $(".content").html(data);
    }
});
}, 3000);