Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

This question already has an answer here:

please help me out in passing php variables into javascript variables. here s my code

<body>
<?php
$yr="2013";
$y=(int)$yr;
$mo="06";
$m=(int)$mo;
$da="04";
$d=(int)$da;
$hr="14";
$h=(int)$hr;
$mi="30";
$min=(int)$mi;
?>
<script type="text/javascript">
var current="Logout";        
var year=<?php echo $y;?>;
var month=<?php echo $m;?>;          
var day=<?php echo $d;?>;           
var hour=<?php echo $h;?>;          
var minute=<?php echo $min;?>; 
....
// displaying js variables....
....
<script>
</body>

out put is showing like NaN:NaN:NaN:NaN

Im New to programming please help me and thanks in advance

share|improve this question

marked as duplicate by Benjamin Gruenbaum javascript May 19 '14 at 15:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
1. How does your display js variables code look? 2. How does your final javascript work after compiling? (use View Source). – h2ooooooo Jun 4 '13 at 8:51
1  
downvotes without any comment? How can new user of SO improve himself? – MikroDel Jun 4 '13 at 8:51
    
I hope this could help you,stackoverflow.com/questions/5310216/… – dreamweiver Jun 4 '13 at 8:51
    
Is this the actual code you're using? It should be noted that you're setting the variable $m to both month and minute, so it will have the value of the minute (30) when you assign it to the javascript variables – Pudge601 Jun 4 '13 at 9:00
    
here is my display block <td align="center" ><div class="numbers" id="dmin"></div></td> <td align="center" ><div class="numbers" id="dsec"></div></td> ..... document.getElementById('dmin').innerHTML=dmin; document.getElementById('dsec').innerHTML=dsec; – user2372177 Jun 4 '13 at 9:12

2 Answers 2

<body>
<?php
$yr="2013";
$mo="06";
$da="04";
$hr="14";
$mi="30";
?>
<script type="text/javascript">
var current="Logout";        
var year=<?php echo $yr;?>;
var year=parseInt(year);  // try this
var month=parseInt(<?php echo $mo;?>); // or this         
var day=parseInt(<?php echo $da;?>);           
var hour=parseInt(<?php echo $hr;?>);          
var minute=parseInt(<?php echo $mi;?>); 
....
// displaying js variables....
....
<script>
</body

This should probably work

http://www.w3schools.com/jsref/jsref_parseint.asp

share|improve this answer

To convert a string to int in PHP use intval

But in your case you don't need to convert, you can just echo it to javascript.

$y = "2013";
$m = "06";

...

var year=<?php echo $y;?>;
var month=<?php echo $m;?>;

Will output

var year=2013;
var month=06;
share|improve this answer
    
Typecasting as an int works just as well as intval. The only difference is that intval can convert to a different base. Casting as an int is also faster. – h2ooooooo Jun 4 '13 at 8:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.