JS location load after function - Add delay time - javascript

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;

Related

Is it legal to combine JS and PHP ... Is that PHS or JHP?

Here I have an example... It works fine, but is it legal?
<script>
setTimeout(function() {
$('.hide_3sec').fadeOut('fast');
<?php
if(isset($_GET["msg"])){
?>
window.history.replaceState({}, document.title, "/admin/" + "?tab=accounts");
<?php
}
?>
}, 3000); // <-- time in milliseconds
</script>
Yes, it's actually a quite common practice in order to pass server variables into the UI. Is it the best practice? I'm not qualified enough to answer that.
Example:
<script>
pageNumber = <?php echo $PAGE_NUM ?>
</script>
In your case, you have an entire if statement print out. It would be simpler to make the if statement always print out, but the boolean that's checked be printed by PHP.
<script>
setTimeout(function() {
$('.hide_3sec').fadeOut('fast');
if(<?php echo var_export(isset($_GET["msg"])); ?>){
window.history.replaceState({}, document.title, "/admin/" + "?tab=accounts");
}
}, 3000); // <-- time in milliseconds
</script>

Disable Particular Modal On Particular Page

I Have A File named as modal.php Which Contains Two Modal
Login Modal
Feedback Modal
Login Modal Runs in Every 5 Seconds if user isn't logged in.and feedback modal opens on click.
And on My website i have 20 pages and in all pages i have included modal.php So the problem i getting is in those 20 pages i have a page named with signin.php. i also include here an modal.php
So here is code for signin so i dont want to show signin modal here.
So how can i do that ?
Modal Opening Code in modal.php is
$(window).on("load", function () {
<?php if(!isset($_COOKIE['loginuser'])){ ?>
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
And I Tried
<?php if(basename(__FILE__) != 'free-listing-new.php') { ?>
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
<?php } ?>
But This Not working and modal will be shown. i think its maybe because file is included. So What do i have to do
I think your trouble lies someplace else. Your code works. Here is how I tested the same. I included the modal in a modal.php only.
<script>
$(window).on("load", function () {
<?php if(!isset($_COOKIE['loginuser'])){ ?>
console.log($('#signup_mask_popup'));
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
<?php
$cookie_name = "loginuser";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<?php }else { ?>
console.log("cookie is set.");
<?php } ?>
<?php if(basename(__FILE__) != 'free-listing-new.php') { ?>
console.log("here");
setTimeout(function() {
$('#signup_mask_popup').modal("show");
}, 5000);
<?php } ?>
});
</script>

Ajax Auto refresh - Table not refreshing

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

Auto reload div contents with data from separate page with jquery

I am trying to reload a page and retrieve data automatically after 10 seconds or preferably less than that. Please i tried so many codes and it didn't work.
Here is what i am using now...
// script //
<script>
$(document).ready(function(){
// setInterval(function() {
// $("#load_msgs").load("load.php");
// }, 10000);
//var updateInterval = setInterval(function() {
//$('#load_msgs').load('load.php?p=<?php echo $username; ?>');
//},1000);
//});
var autoLoad = setInterval(
function ()
{
$('#load_msgs').load('load.php').fadeIn("slow");
}, 10000);
});
</script>
// php / html div id panel
<div class = 'container me' id = "load_msgs"> </div>
// php load.php file
<?php
include('config.php');
include('func.php');
//$username = $_GET['q'];
$o = "--DATABASE QUERY--";
$z = mysql_query($o) or die(mysql_error());
while ($roow = mysql_fetch_array($z)) {
$date = $roow['date'];
echo $roow['message']. " <span class = 'lil'>" . time_elapsed_string($date)."</span> \n <br />";
}
?>
I'm assuming from your comments that you can navigate directly to load.php and the data is echo'd. If that is the case, here's how I would set it up:
(It's not clear if you trying to load a file every X seconds, or just once after X seconds, Here is an example of both)
index.php:
<?php
$username='someUser'
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Load From PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var username = encodeURI( <?php echo isset($username) ? "'".$username."'" : ''; ?> );
$(function(){
var url='load.php?username='+username;
// load once after 2 seconds
setTimeout(function(){ $.get(url, function(response){ $('#load_msgs').html(response); } ); }, 2000);
// load every 2 seconds
setInterval(function(){ $.get(url, function(response){ $('#load_msgs_repeat').append(response+'<br>'); } ); }, 2000);
});
</script>
</head>
<body>
<h4>Div loaded once</h4>
<div id="load_msgs"></div>
<br>
<h4>Div loaded every 2 seconds</h4>
<div id="load_msgs_repeat"></div>
</body>
</html>
load.php:
<?php
$username= isset($_GET['username']) ? $_GET['username'] : null;
echo $username ? 'here is some data loaded from load.php for user: '.$username : 'here is some data loaded from load.php, but no user was provided' ;
?>
Here is a functioning example
The problem may be in config.php or func.php. Are you connecting to the database before running your query?
Add these two lines to the start of load.php:
ini_set("display_errors", "On");
error_reporting(E_ALL);
Then visit load.php in your browser. You should get some helpful error messages.
window.setTimeout(function(){window.location.replace("page.php")},5000);

Javascript - How to load variable from a file and refresh it every 3 seconds?

My script:
<?php
//header("refresh: 7;");
include 'theme.php';
ceklogin();
css();
echo '<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(
function() {
setInterval(function() {
var randomnumber = Math.floor(Math.random() * 100);
$("#show").text(
"I am getting refreshed every 3 seconds..! Random Number ==> "
+ randomnumber);
}, 3000);
});
</script>';
exec('/www/openvpn_wget_status.sh');
echo '<br>';
echo file_get_contents( "/www/openvpn_status.txt" );
echo '<div id="show">';
//include 'wget.log';
//echo "<br><textarea name=\"text-info\" id=\"scores\" rows=\"30\" cols=\"90\" readonly style=\"font-family: Arial;font-size: 7pt;\" >";
//$datalines = file ("wget.log");
//foreach ($datalines as $zz) {
//echo $zz; }
echo '</div>';
//echo "</textarea><br></div>";
echo '<script type="text/javascript">
var textarea = document.getElementById("scores");
textarea.scrollTop = textarea.scrollHeight;
</script>';
foot();
echo '
</div>
</body>
</div>
</html>';
?>
it works perfectly but what if I want the variable from a log file in my webserver, let's say the name of the log file is wget.log and I want to refresh it every 3 seconds since wget.log keeps changing as I run wget to download files?
Script to read file (readFile.php)
<?php
//in this script, read any file(or do something else)
//whatever you output, it will be processed in ajax request from below script
echo "<pre>".file_get_contents( "/www/openvpn_status.txt" )."</pre>"; //used <pre> to keep linebreaks in file.
?>
HTML (another file)
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(
function() {
setInterval(function() {
$("#show").load("readFile.php"); //it will make ajax call to readFile.php and get output from that script and load to #show div
}, 3000);
});
</script>
<div id="show">
This will be refreshed every 3 seconds.
</div>

Categories