I'm having a serious problem in my code and I can't manage to fix it.
I'm currently using Ajax to setTimeout and get the data from it.
The problem here is that it's not refreshing (if I go the page getdata.php via url it refreshes but I wanted to refresh without going to that page).
This is my current ajax script:
<div id="tableHolder"></div>
<script>
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('getdata.php', function(){
setTimeout(refreshTable, 1000);
});
}
</script>
The code in getdata.php is the following one:
<?php
require_once("config.php");
$req2 = mysqli_query($con, 'select user_id, user_name from users LIMIT 5');
while ($dnn = mysqli_fetch_array($req2)) {
echo "<table>";
echo "<tr>";
echo "<td>" . $dnn['user_id'] . "|</td>";
echo "<td>" . $dnn['user_name'] . "</td>";
echo "</tr>";
echo "</table>";
}
?>
I can't find any solution to this problem so my last hope is on you StackOverflow!
The issue is because the $.load method is serving you the cached data. You need to add some random text in url like timestamp
You can use like this
<script>
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('getdata.php?time=' + new Date().getTime(), function(){
setTimeout(refreshTable, 1000);
});
}
</script>
Instead of setTimeout you can use setInterval with some querystring
function refreshTable(){
setInterval(function(){
$('#tableHolder').load('getdata.php?v=1')
}, 1000);
}
Settimeout is usually used for one-time execution
Try setInterval instead if you want to repeat calling load function
Related
I'm using jquery-3.3.1.min to live update and calling it to my main.php
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function () {
$('#show').load('smoke.php')
$('#show2').load('pids.php')
$('#show3').load('flame.php')
$('#show4').load('panic.php')
}, 2000);
});
/////
I'm echo in php using
$smoke_status = $row['smoke_status'];
$pids_status = $row['pids_status'];
$flame_status = $row['flame_status'];
$panic_status = $row['panic_status'];
$startdate = $row['startdate'];
$stopdate = $row['stopdate'];
echo "<tr>";
echo "<td id='show'></td>";
echo "<td id='show2'></td>";
echo "<td id='show3'></td>";
echo "<td id='show4'></td>";
echo "<td>$startdate</td>";
echo "<td>$stopdate</td>";
echo "</tr>";
but now...i want to make my live update data into a variable in script
how can i declare it.
i'm success calling this
var i = <?php echo $panic_status ?>;
and how can i call
echo "<td id='show'></td>";
echo "<td id='show2'></td>";
echo "<td id='show3'></td>";
echo "<td id='show4'></td>";
into new variable??plsss help and srry for the long question
Hello I do not know what you are actually doing with the code. its so scattered that one cannot easily know where to start
1.) You are displaying a result and i did not see where you are trying to escape those database with htmlentities() or htmlspecialchars() functions against xss attack.
2.) I do not know whether you are still using mysql_connect deprecated functions in your database query. if so please better move it to mysqli or PDO.
3.) i can see you calling simultaneously four php files simultaneously with 2 seconds call. What is it you are trying to achieve. This will cause alot of issues to the server including latency, poor performance and over consumption
of data. If you need a real time update why don't you switch over to nodejs and socket.io.
To answer your question, I have created an ajax example to get you started
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
//$('#result').click(function(){
var post1 = 'data to post if any';
$('#loader').fadeIn(400).html('Please Wait. Data is being Loaded');
// assuming that you want query result by posting a variable
var datasend = "alert1="+ post1;
$.ajax({
type:'POST',
url:'smoke.php',
data:datasend,
crossDomain: true,
cache:false,
success:function(msg){
$('#loader').hide();
$('#result').fadeIn('slow').prepend(msg);
}
});
//})
});
</script>
<div id="loader"></div>
<div id="result"></div>
The above script will make a call to smoke.php and display any result contained there in
<?php
$smoke_status = 'I am not smoking';
/* if data is to be displayed in html form, you have to escape it with either htmlspecialchars() or htmlentities() functions to ensure
that XXS attack is not possible. you can read further on how to escape both single, double quotes with it as case may be
*/
echo htmlspecialchars($smoke_status);
?>
I have the following script in a HTML file that is called when the document is loaded:
<script>
$(document).ready(function(){
setInterval(function() {
$.get('check_session.php', function(data) {
alert('Load was performed.');
});
}, 5000);
});
</script>
The alert message will be called on the interval, but the PHP is not actually called because nothing is echoed.
PHP file: check_session.php
<?php
//check_session.php
session_start();
echo '<script language="javascript">';
echo 'alert("successful")';
echo '</script>';
echo $_SESSION['user_token'];
if(isset($_SESSION['user_token']))
{
echo '0'; //session not expired
}
else
{
echo '1'; //session expired
}
?>
Essentially, I am trying to call the PHP file, check_session.php, on a five second interval. This is one of my first times implementing jQuery, and after much research, I am still lost.
Any suggestions as to why the php file is not called are appreciated.
---UPDATE:
From Network tab:
Check data parameter
<script>
$(document).ready(function(){
setInterval(function() {
$.get('check_session.php', function(data) {
alert('Load was performed.');
console.log('data: '+data)
});
}, 5000);
});
</script>
the problem might be here: $.get needs URL and other optional params. you are simply giving filename. I think you should add appropriate path also. try it !!
I have made a function.php and I included a redirect (to another page) after processing the function.
It works great - I click / function executes and it redirects.
BUT
I want it not to instant redirect and to delay a couple of seconds, since I have a message "Succesuful added.. "
Last part it includes the JS:
//Display ID
echo "<div align='center'> <h4>*Command completed! NR: ".$id_receptie. </h4></div>";
echo "<script type='text/javascript'>
window.onload = function () { top.location.href = 'fisa_service.php?id=" . $id_receptie . "'; };
</script>";
Try the setTimeout function.
setTimeout(function(){
// redirect
}, 2000);
The 2000 are the seconds in milliseconds.
Hope this helps.
You can use the sleep() function in PHP : http://php.net/manual/fr/function.sleep.php
Try next script:
<?php
$timeout = 2; // Timeout in the seconds
$id_receptie = 0;
?>
<div align='center'><h4>*Command completed! NR: <?= $id_receptie ?> </h4></div>
<script type='text/javascript'>
setTimeout(function() {
top.location.href = 'fisa_service.php?id=<?= $id_receptie ?>';
}, <?= $timeout ?>*1000);
</script>
<?php
exit;
I have a dynamically generated post, where i use ajax to find, the no of comments and likes in it. so, i gave a onmouseovr selector and called the js function. but i want it to automatically be called once every 2 seconds. how should i do it?
echo "</div>";
//the div has to be updated every 2-3 seconds.
//but i dont know how. so, i gave a mouseoverfunction.
echo '<div onmouseover="yopahshareloader(';
echo "'" .$id. "',";
echo "'" .$postype. "'";
echo ');" id="'.$id.'_action" style="height:20px;width:100%;margin-bottom:5px;overflow:hidden;">' ;
echo '</div>';
Use setInterval
<script type="text/javascript">
window.setInterval(function() {
yopahshareloader('<?php echo $id; ?>', '<?php echo $postype; ?>');
}, 2000);
</script>
In my PHP I am returning and updating a online users box however as it Echos each line out I also want it to activate a Jquery function. So if there is data there it will then echo it out and a Jquery function will take place.
example of php code ...
if($count1 > 0) {
foreach (user_list($user_name) as $user){
echo $user . "<br />";
};
echo ;// a message to activate a jquery function called userdisplay;
};
and the jquery code would look something like this...
function userdisplay(){
//do some amazing code like slide picture in from left fade text in at the top etc.
};
(I am not asking for help on the code which the function will do this is just an example)
Many thanks to anyone with an idea of how i should go about this my mind is completely blank.
just echo the call to the jquery function between a <script> tagfor example:
this is the page:
<html>
<head>
<script>
$(document).ready(function(){
//some jquery code here
});
//here is your function
function userdisplay(){
alert("triggered");
}
</script>
</head>
<body>
....
<?php
if($count1 > 0) {
foreach (user_list($user_name) as $user){
echo $user . "<br />";
};
echo "<script>userdisplay();</script>";
};
?>
....
</body>
</html>
if($count1 > 0) {
foreach (user_list($user_name) as $user){
echo $user . '<br />';
};
echo '<script>userdisplay();</script>';
};
You just have to make sure the function userDisplay() has been "echoed" before.