Reputation: 359
I am trying to create a document title timer function which reflect the selected login time the user choose using a drop down list.
So far i have gotten the timer working with a default time of 30 seconds and i am having problem linking the drop down "option value" to my javascript.
What i dont understand is that wont my variable $droptime dynamically grab the value from the drop down "option value" ?
index.php
<form id="form1" align="left" method="POST" action="second.php">
<select name ="droptime">
<option id="5" value="5" >5 Seconds</option>
<option id="10" value="10">10 Seconds</option>
<option id="15" value="15">15 Seconds</option>
<option id="30" value="30">30 Seconds</option>
</select>
</form>
This is my javascript for the document title
second.php
<?php
session_start();
$drop_time = $_SESSION['droptimeD'];
?>
<script language="javascript" type="text/javascript" src= "index.php">
<!-- //
var m = <?php echo $drop_time; ?> ;
var i = new Number(m);
function titleTimer(){
document.title = "Time till logout - " + i + " seconds.";
i -= 1;
if(i < 0){
document.title = "Please login Again"
window.location.replace("index.php");
}else{
setTimeout("titleTimer()", 1000);
}
}
// -->
</script>
I apologies if there might be a duplicate question. I did search through the forum and tried multiple solution to no available and i suspect i am missing a crucial part to get it to work.
Upvotes: 1
Views: 136
Reputation: 359
Its abit weird but i was parsing data all over the place plus the confusion. Also i mixed up php and java script context
index.php
<form id="form1" align="left" method="POST" action="second.php">
<select name ="droptime">
<option id="5" value="5" >5 Seconds</option>
<option id="10" value="10">10 Seconds</option>
<option id="15" value="15">15 Seconds</option>
<option id="30" value="30">30 Seconds</option>
</select>
</form>
This is my javascript for the document title
second.php
<?php
session_start();
$drop_time = $_SESSION['droptimeD'];
?>
<script language="javascript" type="text/javascript" src= "index.php">
<!-- //
var m = <?php echo $drop_time; ?> ;
var i = new Number(m);
function titleTimer(){
document.title = "Time till logout - " + i + " seconds.";
i -= 1;
if(i < 0){
document.title = "Please login Again"
window.location.replace("index.php");
}else{
setTimeout("titleTimer()", 1000);
}
}
// -->
</script>
Upvotes: 0
Reputation: 3854
As mesutozer said, you can't use the php variable in javascript. You have to grab the value from the html document using something like
var i = document.getElementsByName('droptime')[0].value;
Upvotes: 3