You can also use the js helper:
<?php
// view code
$sample = array('sample' => 1, 'is' => 2, 'crazy' => 3);
echo $html->scriptStart(array('inline' => false));?>
var myvar = <?php echo $js->object($sample);?>;
<?php echo $html->scriptEnd();?>
On Apr 12, 2:21 pm, mookatt <[email protected]> wrote:
> There are quite a few ways of doing this. I typically create HTML
> elements and set their value or contents then access the element from
> javascript.
>
> php:
> <?php
> $v1=5;
> $v2=array('item1'=>array('item1.1','item1.2','item1.3'),'item2'=>array('item2.1','item2.2'));
> );
>
> echo "<div style='display:none' id='v1'>$v1</div>";
> echo "<div style='display:none' id='v2'>".json_encode($v2)."</div>";
> ?>
>
> Then in javascript:
> var v1=document.getElementById("v1").innerHTML;
> var v2=JSON.decode(document.getElementById("v2").innerHTML); //
> requires the window.JSON object or a framework such as mootools/jquery
> that provides it
>
> On Apr 12, 6:12 am, Shashank <[email protected]> wrote:
>
> > Hey Guys,
>
> > I wanted to know that how can i pass or set a varibale in a view file
> > to be used in some js file.
>
> > Thanks
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php