The script below is for a live question moderation system I'm developing. Someone could give me a hint of how I do so that the script below refresh the query and refresh the result automatically, it can be every 30 seconds. Maybe some solution in Ajax?
<?php
global $wpdb;
if (!empty($_POST)) {
$wpdb->query($wpdb->prepare("UPDATE wp_modera SET answered = 'Yes' WHERE id='" . $_POST['id']. "'" ));
}
?>
<script>
jQuery(document).ready(function($){
$("#form1").on("change", "input:checkbox", function(){
$("#form1").submit();
});
setInterval(function(){
$.get('#form1', function(data){
//$("#form1").html(data).find('#form1');
});
},3000);
});
</script>
<style>
table {width: 100%;}
table tr td {border-bottom: 1px solid #666; padding: 20px 0;}
table tr td:first-child {padding-right: 20px;}
table tr td .title {color: #315AA9;}
</style>
<form id="form1" method="post" action="http://example.com/test/" >
<table>
<?php
global $wpdb;
$results = $wpdb->get_results( 'SELECT id, time, city, state, question, name FROM wp_modera WHERE release = "Yes" AND flag2 <> "PLT" AND answered = "No" ORDER BY time DESC', OBJECT );
foreach($results as $result){
echo "<tr>";
$phpdate = strtotime($result->time );
$mysqldate = date( 'H:i:s', $phpdate );
echo "<td><strong class='title'>" . $mysqldate . " - " . $result->name . " - " . $result->city . " / " . $result->state . '</strong><br><br> <strong>Question: </strong>' . $result->question . "</td>";
echo "<td><input type='checkbox' name='id' value='" . $result->id . "' /></td>";
echo "</tr>";
}
?>
</table>
</form>