Jump to content

Pass a variable from PHP to Javascript


arenaninja

Recommended Posts

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.

Link to comment
Share on other sites

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


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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}
.....);})

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.