Solved

How to after x seconds run a routine and redirect to other page?

Posted on 2011-03-25
3
278 Views
Last Modified: 2012-08-14
Hi there,

i need a code in a a file msg.php, per example, after x seconds of displaying a warning, it change a value in the database and redirect the page for index.php, per example?

how to do this the best way?

Best regards,
0
Comment
Question by:AndreMarques
3 Comments
 
LVL 5

Expert Comment

by:meb_santoso
ID: 35216683
i don't know what's the BEST way, but you can try this :

// display warning here
 ...your routine goes here...
sleep(10); // in seconds
// change a value
....insert your own routine here (I dunno what's you're trying to change)....
// go to index.php
header('Location: http://www.example.com/index.php'); // or whatever location you need
0
 
LVL 5

Expert Comment

by:tsmgeek
ID: 35217764
if its for browsers then you need to do it diffrently

display the warning page then have a js & meta-refresh tag to redirect to another page (or same page) with extra query param to then apply the DB change
0
 
LVL 2

Accepted Solution

by:
kanchan_karjee earned 250 total points
ID: 35231135
If this is your requirement
-- Display a warning message to user say for 10 sec.
-- Redirect the user after database updatation etc.

Then you could try using ajax.
update.php is the script where you update database.
Waiting time can be modified as requiered


<head>
<script type="text/javascript">
var redirect = false;
var waitingTime = 0;
var messageDisplayTime = 10;
function updateDBTask() {
      var xhr;
      if (window.XMLHttpRequest) {
            xhr=new XMLHttpRequest();
      } else {
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xhr.onreadystatechange=function() {
            if (xhr.readyState==4 && xhr.status==200) {
                  redirect = true;
            }
      }
      xhr.open("GET","update.php",true);
      xhr.send();
}

function Redirect() {
      waitingTime++;
      if (redirect == true) {
            if(waitingTime >= messageDisplayTime) {
                  clearTimeout(t);
                  document.write('<scr'+'ipt type="text/javascript" >location.href="index.php"</scr'+'ipt>');
            }
      }
}
</script>
</head>
<body>
<h2>Warning message here...</h2>
</body>
<script>
updateDBTask();
var t = setInterval("Redirect()",1000);
</script>
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now