0

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

  • 1. How does your *display js variables* code look? 2. How does your final javascript work after compiling? (use *View Source*). – h2ooooooo Jun 04 '13 at 08:51
  • 1
    downvotes without any comment? How can new user of SO improve himself? – MikroDel Jun 04 '13 at 08:51
  • I hope this could help you,http://stackoverflow.com/questions/5310216/passing-php-variable-into-javascript – dreamweiver Jun 04 '13 at 08: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 04 '13 at 09:00
  • here is my display block
    ..... document.getElementById('dmin').innerHTML=dmin; document.getElementById('dsec').innerHTML=dsec;
    – user2372177 Jun 04 '13 at 09:12
  • how you are displaying the numbers? – MMK Jun 04 '13 at 09:13
  • There's no such variables as `dmin` and `dsec`. – h2ooooooo Jun 04 '13 at 09:22
  • Im sorry i missed few statements var dmin=Math.floor(((tsecs%(60*60*1000*24)) %(60*60*1000))/(60*1000)*1); var dsec=Math.floor((((tsecs%(60*60*1000*24))%(60*60*1000)) %(60*1000))/1000*1); where tsecs is total secs got throuh a formula and sure it holds an integer value. – user2372177 Jun 04 '13 at 09:35
  • code you posted first should just work. Please show us how how you display the variables. Please EDIT your question, if you want to add something in it. – nl-x Jun 04 '13 at 10:23

2 Answers2

1
<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

dk396
  • 91
  • 1
  • 2
0

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;
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • Typecasting as an `int` works just as well as `intval`. [The only difference is that `intval` can convert to a different base](http://stackoverflow.com/questions/1912599/is-there-any-particular-difference-between-intval-and-int). Casting as an `int` is also faster. – h2ooooooo Jun 04 '13 at 08:55