Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would pass two php variables from a codeigniter controller to a javascript function located to the head of my view. is it possible to do such thing?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

yeah sure, just echo the javascript: <?php echo '<script>var myPhpVariable = "'. $my_var . '";</script>'; ?>

share|improve this answer
 
what? so easy...I didn't think it should be so easy...wow...thanks... –  eng_mazzy Feb 23 '12 at 1:16
1  
Remember JS is executed server side from within the HTML document, the PHP is processed before all of that, so you can always print out any JS you need to have done on the fly, as in this answer –  Ben Feb 23 '12 at 1:29
1  
@BenGriffiths client side that is... –  giorgio Feb 23 '12 at 9:21
1  
Haha yes, I have no idea why I wrote server side! –  Ben Feb 23 '12 at 11:09
 
What if i need to pass an entire array, or lets extend it, a multidimensional array... Would this approach work then? –  Nibhrit Sep 26 at 10:45
add comment

or just set in your controller your variables

eg - in the controller

$data['newvar'] = $myvar_in_the_controller;

and then in the view in the javascript

<?php echo $newvar ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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