Calling javascript from inside PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raydona
Forum Newbie
Posts: 21
Joined: Sat Mar 21, 2009 9:17 am

Calling javascript from inside PHP?

Post by raydona »

Hi,
How can I jump (redirect) to another page after say 6 seconds from inside PHP code. For example, I wish to say:

Code: Select all

 
$sql = "some code"; 
if(mysql_query($sql)) 
{ echo "Successful<br/>";
  //redirect to page A after 6 seconds
}
else 
{ echo "Unsuccessful";
  //redirect to page B after 6 seconds 
}
The only way I know how to redirect to another page after some time is using javascript.
<script type="text/javascript">
  function timedMsg()
  { var t = setTimeout("location.href='file.html'",6000);
  }
</script>
 
But how can I write javascript inside PHP code? I would be very grateful for all help.
Last edited by Benjamin on Mon Apr 27, 2009 8:23 pm, edited 1 time in total.
Reason: Added code tags.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Calling javascript from inside PHP?

Post by Christopher »

PHP is a language designed to generate HTML pages. Anything added to the output by echo() and header(), and anything outside of the <?php ?> tags will be sent to the browser.
(#10850)
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Calling javascript from inside PHP?

Post by Yossarian »

Make sure you have previously echoed absolutely nothing to the browser, not even a blank line or a space.

If so, as I understand it you can then sleep() for x seconds and redirect using php's header() command something like this;

Code: Select all

 
if(something-bad) {
 
sleep($time);
header( 'Location: ' . $location );
exit();
 
}
I've never used sleep before, so try it without first off.

EDIT
I am not at all sure that is what you want to do though, if you want to show a six second message then your should redirect to six-second-message.htm which contains a html meta tag redirect, or the js timer and redirect.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Re: Calling javascript from inside PHP?

Post by user___ »

Why do not you try META?
<meta http-equiv="Refresh" content="[time]; URL=[some-url]">
theo13
Forum Newbie
Posts: 2
Joined: Tue Apr 28, 2009 8:44 am

Re: Calling javascript from inside PHP?

Post by theo13 »

user___ wrote:Why do not you try META?
<meta http-equiv="Refresh" content="[time]; URL=[some-url]">
Are you sure it will work with META?( I personally try to avoid it- if it works it would be an alternative) :D :D :D

The easiest way(not necessarily the best) is to call javascript is simply to use echo within the php code.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Re: Calling javascript from inside PHP?

Post by user___ »

Yes, I am sure it will work, but I can not say the same for the JS solution, because if a client has his/hers JS off, no redirection will be made.
Post Reply