Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,414 members, 7,954,651 topics. Date: Saturday, 21 September 2024 at 04:58 AM |
Nairaland Forum / Science/Technology / Webmasters / Passing Php Variables To Javascript (752 Views)
Welcome To Javascript For The Total Non- Programmer / Dhtml Quiz: How Will You Detect If A Browser Support Javascript From Php Code? / Session Variables (2) (3) (4)
Passing Php Variables To Javascript by DualCore1: 10:02pm On Jun 07, 2010 |
Anyone has any method he/she uses to accomplish this? The scenario is I have a php document containing some variables and I need to do some calculations in a seperate javascript file using those php variables. I have already achieved this but I am trying to optimize my codes so i'm looking to know if there's a better way anyone has of doing this. Here's how I do it. I use html as the bridge. I include the php document into html and pass the variables into hidden form fields <input type="hidden" value ="<?php echo $variable1 ?>" id = "variable1" /> In javascript I now get the variable from the hidden field in the html. var variable1 = parseInt($('#variable1').val()); And then I can do any math I want. Now imagine crossing about a hundred variables from php to javascript through my kinda bridge! That's too many kids on the bridge!! I know there must be a better way, any ideas? |
Re: Passing Php Variables To Javascript by ogzille(m): 11:02pm On Jun 07, 2010 |
<script language="javascript">var e='<?php echo $variable;?>';</script> |
Re: Passing Php Variables To Javascript by kehers(m): 11:05pm On Jun 07, 2010 |
Hmmm, Another way I can think of is masking ur php as js and passing the variables to it via GET like this HTML: <script type="text/javascript" src="js/js.php?var=value&var2=value2"></script> JS (js.php): <?php header('content-type:application/x-javascript'); foreach($_GET as $k => $v){ $$k = $v; } ?> var variable1 = <?= $var; ?>; var variable2 = <?= $var2; ?>; The drawback however, is that it is assumed the variables are already declared before including the js. |
Re: Passing Php Variables To Javascript by OmniPotens(m): 2:54am On Jun 08, 2010 |
Re: Passing Php Variables To Javascript by DualCore1: 5:08am On Jun 08, 2010 |
Thanks guys |
(1) (Reply)
I Need Help With An Assignment / Somebody Please Help Me / How Can I Design A Website With Joomla Installation
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8 |