Jump to content

Calling javascript from inside PHP?


raydona

Recommended Posts

Hi,

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

$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.

 

Link to comment
Share on other sites

$sql = "some code";
if(mysql_query($sql))
{ echo "Successful<br/>";
  $page = "pagea.html";
}
else
{ echo "Unsuccessful";
  $page = "pageb.html";
}
?>
<script type="text/javascript">
  function timedMsg()
  { var t = setTimeout("location.href='<?php echo $page; ?>'",6000);
  }
</script>

Link to comment
Share on other sites

Hi,

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

$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.

 

 

<?php
$sql = "some code";
if(mysql_query($sql))
{
    echo "Successful<br/>";
    sleep(6); //6 seconds
    header("Location: newPage.php");
}
else
{ 
    echo "Unsuccessful";
    sleep(6); //6 seconds
    header("Location: newPage.php");
}
?>

Link to comment
Share on other sites

<?php
$sql = "some code";
if(mysql_query($sql))
{
    echo "Successful<br/>";
    sleep(6); //6 seconds
    header("Location: newPage.php");
}
else
{ 
    echo "Unsuccessful";
    sleep(6); //6 seconds
    header("Location: newPage.php");
}
?>

 

Will not work. Output has been sent to the browser :) Alternatively he could use a META tag instead of Javascript, but yea.

Link to comment
Share on other sites

<?php
$sql = "some code";
if(mysql_query($sql))
{
    echo "Successful<br/>";
    sleep(6); //6 seconds
    header("Location: newPage.php");
}
else
{ 
    echo "Unsuccessful";
    sleep(6); //6 seconds
    header("Location: newPage.php");
}
?>

 

Will not work. Output has been sent to the browser :) Alternatively he could use a META tag instead of Javascript, but yea.

 

Doh, good catch.

Link to comment
Share on other sites

This code works for me.  I use it for all my redirects.

 

<?php
$location = 'http://www.someaddress.com';
header("Refresh: 5; URL=\"$location\"");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">                
        <title>Successful upload</title>    
    </head>    
    <body>    
        <div id="Upload">
            <h1>Successful File Upload</h1>
            <p class="centeralign">Congratulations! Your file upload was successful</p>
            <p class="centeralign">You will be redirected in a few seconds...if not click <a href="http://www.someaddress.com">here</a></p>
        </div>    
    </body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.