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
https://forums.phpfreaks.com/topic/155926-calling-javascript-from-inside-php/
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>

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");
}
?>

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

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.