Page 1 of 1
Javascript alert box with PHP once per browser session
Posted: Thu Nov 24, 2005 7:38 am
by paulng
I have this javascript that displays an alert box on my home page for users to download a software when click ok, I have been trying to write a session cookie in PHP that display the alert once box per browser session.
here is the javascript code for the alert box
Code: Select all
function getdownload(){
if (confirm("<?php echo("$message"); ?>")) {
window.location.href='../directoty/file.txt';
}
}
and here is my php cookie code that isnt working
Code: Select all
<?
if (!$_COOKIE['visited']) {
setcookie("visited", "true");
echo "onload=javascript:getdownload();";
}
?>
can anyone help with a better solution...?
Posted: Thu Nov 24, 2005 8:36 am
by Grim...
You should set the cookie before any html is outputted.
Code: Select all
<?php
if (!$_COOKIE['visited']) {
$show = 1;
setcookie("visited", "true");
}
?>
<html>
<head>...Javascript and stuff...</head>
<body <?php
if ($show)
{
print "onload=javascript:getdownload();";
}
?>>...body stuff...</body>
</html>
Posted: Thu Nov 24, 2005 9:58 am
by paulng
I've tried what you said but it's not working, is not producing any error at the same time the alert message session cookie is not displaying per browser session.
Posted: Thu Nov 24, 2005 10:29 am
by Grim...
Take a look in the source - is the onload event there on a new session?
Posted: Thu Nov 24, 2005 11:05 am
by paulng
Yep the onload event is in the source, but still no effect...

Posted: Thu Nov 24, 2005 11:35 am
by Grim...
Well, that's a problem with the javascript.
Let's take a look...
Shouldn't it be:
Code: Select all
print "onload=\"getdownload();\"";

Posted: Fri Nov 25, 2005 4:00 am
by paulng
I've tried...still give the same problem let me try to explain everything again
here is my index file
Code: Select all
<?php
if (!$_COOKIE['visited']) {
$show = 1;
setcookie("visited", "true");
}
?>
<?php
include("head.php"); ?>
<!---------------- end : head -->
<body <?php
if ($LANG == "en" || $LANG == "sw" || $show){
?>onLoad="getdownload();"<?php
}
?>>
<!---------------- start : LAYOUT -->
<?php include("l/$LAYOUT/php/hplayout.php"); ?>
<!---------------- end : LAYOUT -->
</body>
</html>
the onload javascript function is in the header.php as follow
Code: Select all
function getdownload(){
if (confirm("<?php echo("$message"); ?>")) {
window.location.href='../download/software.exe;
}
}
the alert box is working fine , it just the once per browser session cookie that's not working.... Im sure it something simple it just needs to be figured out...
