I have a checkbox and everytime I click on it, the state is recorded into a MySQL database. How would it be possible to refresh the state of the checkbox every 5 seconds (using Ajax - setInterval), taking the state value from the database?
At the moment, the code looks like this:
<div class="onoffswitch">
<input type="hidden" id="hidden1" value="6">
<input type="checkbox" name="onoffswitch1" class="onoffswitch-checkbox" id="myonoffswitch1"
<?php
$query3=mysql_query("select * from choice where id=6");
$query4=mysql_fetch_array($query3);
if($query4['choice']=="1")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="myonoffswitch1">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
You can setup an ajax process in interval. Before using that code, make sure jQuery library has been included to the page. So:
<script type="text/javascript">
setInterval(function(){
$.ajax({
type: 'POST', /* Possible GET */
data: {...},
url: 'file.php' /* Relative path to file */
success: function(response){
/* do what you wabt with response object **/
}
});
},5000);
</scritp>
Place you database process codes into "file.php" and only return specific data from the file to ajax.