arenaninja Posted March 3, 2012 Share Posted March 3, 2012 Hello all. I'm nearing the end of my application building. It's a simple reservation thingamajeeg and I'm using the jQuery UI date picker (with timepicker add-on). Anyway, I'm currently using the following: $(document).ready(function(){ $('#res_start').datetimepicker({ ampm:true, timeformat:'h:m', stepMinute:15 }); }) The issue here is that I need to set stepMinute depending on a sql result from a $_GET variable. (I pass the 'equip' via $_GET, then lookup the minimum reservation time. This is the value I need to set as stepMinute. Sometimes it's 15, others 30, yet others 60, etc.). Any help is appreciated. And note that I would like this as basic as possible. Many thanks. PS: I should mention that the javascript file is contained separately from my PHP file. Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/ Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 Are you saying that the page with the datepicker form would have a $_GET['equip'] variable(like page.php?equip=xxx), and that you would check something in a database using the 'equip' value, and depending on the results, you need the stepMinute would be 15, 30, or whatever? That's how I read your question. If so, you would call the datetimepicker() function anywhere on the page and then when you get to the 'stepMinute:' part of the function call, you would echo the value, based on whatever your results were from the sql call. So something like // page header stuff <body> // html stuff here <?php $equip = addslashes($_GET['equip']); $query = "QUERY TO ACCESS TABLE BASED ON EQUIP VALUE"; // process query // figure out the needed stepMinute value based on the info grabbed $stepMinute = whatevervaluehere; ?> $(document).ready(function(){ $('#res_start').datetimepicker({ ampm:true, timeformat:'h:m', stepMinute:<?php echo $stepMinute; ?> }); }) // rest of html of page Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1323595 Share on other sites More sharing options...
arenaninja Posted March 4, 2012 Author Share Posted March 4, 2012 That's mostly correct for an interpretation, however my html markup is in a completely separate file from my JavaScript and I'd like to keep them separate. Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1323620 Share on other sites More sharing options...
dannyb785 Posted March 4, 2012 Share Posted March 4, 2012 In most jquery documentation, they recommend making the function call within the html page itself and not in a separate javascript file. There is no way to dynamically change the settings in a .js file. The only way I can think of is to have htaccess create a .js file by using a rewrite rule to access a php file that spits out the javascript code. Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1323626 Share on other sites More sharing options...
nogray Posted March 4, 2012 Share Posted March 4, 2012 You can just add the variable in your html page before you include the scripts, using dannyb785 example, you can just replace the javascript part by <script type="text/javascript"> var my_step_min = <?php echo $stepMinute; ?>; </script> ... later on your js file ... // check if my_step_min is defined, if not, create a default value ..... ..... timeformat:'h:m', stepMinute:my_step_min} .....);}) Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1323873 Share on other sites More sharing options...
dannyb785 Posted March 4, 2012 Share Posted March 4, 2012 You can just add the variable in your html page before you include the scripts, using dannyb785 example, you can just replace the javascript part by <script type="text/javascript"> var my_step_min = <?php echo $stepMinute; ?>; </script> ... later on your js file ... // check if my_step_min is defined, if not, create a default value ..... ..... timeformat:'h:m', stepMinute:my_step_min} .....);}) yessss very true, forgot about that way Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1323907 Share on other sites More sharing options...
Adam Posted March 5, 2012 Share Posted March 5, 2012 In most jquery documentation, they recommend making the function call within the html page itself and not in a separate javascript file. If you're referring to the examples, that's done simply to make them more portable; it's not recommended. Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1324072 Share on other sites More sharing options...
dannyb785 Posted March 5, 2012 Share Posted March 5, 2012 In most jquery documentation, they recommend making the function call within the html page itself and not in a separate javascript file. If you're referring to the examples, that's done simply to make them more portable; it's not recommended. whatever, recommended or not, it works and it the best way to easily modify it instead of keeping it in a separate js file Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1324178 Share on other sites More sharing options...
Adam Posted March 5, 2012 Share Posted March 5, 2012 As dannyb785 quite rightly pointed out though, you're mixing HTML with JS. I mean it depends how much JS you actually have, but besides it being bad practise, you're bloating the mark-up and reducing your code:text ratio, which can be quite damaging to SEO. Of course you're unable to cache the file either, so it wastes bandwidth as well. Quote Link to comment https://forums.phpfreaks.com/topic/258214-pass-a-variable-from-php-to-javascript/#findComment-1324185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.