View Full Version : starting calendar at a known date

05-08-2005, 11:25 AM
Jasons date input calendar


I found the above script really useful but it would be perfect with a slight modification and I would really appreciate some advice on how to do this. I would like to pass a starting date parameter to it. At the moment it defaults to current date which I would like to do if no parameter is passed. This is because I validate the form and if there are errors on other parts of the form then the dates that have been entered are reset to current date and have to be re-input. Also, it says on the heading that it works for all browsers but when you click on it, it only says IE. Does it work for everything else?

If someone can help, I would be very grateful.


05-08-2005, 06:09 PM
I know it is confusing as to which scripts work with which browsers on Dynamic Drive here, I think it is because, not only have several new browsers come out since some of these scripts were published but also, many have been updated to work with these new browsers, while others have not. As a result the whole 'Legend' system for determining a script's browser compatibility is inaccurate in places. However this script is intended to work as stated on the demo page just above the on page demo:

The script works in all modern browsers- IE5+, NS6/ Firefox, Opera 7+. Nice!

Now, about passing a date to the page this script is on, that's easy. It depends on how you wish to pass the date, though. Here is how to write the on page code:


<script>DateInput('orderdate', true, 'DD-MON-YYYY', PassedDate)</script>

</form>I've created a variable, 'PassedDate' and placed it in 'DateInput's' 'DefaultDate' field. For this not to cause an error, you need to put this script in the head:

<script type="text/javascript">
var PassedDate
</script>What you do with that variable when the form doesn't validate is up to you, if it is left declared but, undefined, the script defaults to the current date. If you define PassedDate AFTER or AT THE TIME it is declared, it will make the DateInput date field(s) it is associated with conform to its date. Ex:

<title>Select Date w/passed date Demo</title>
<script type="text/javascript">
var PassedDate='30-Jan-2010'
<script type="text/javascript" src="calendarDateInput.js">

* Jason's Date Input Calendar- By Jason Moon http://www.jasonmoon.net/
* Script featured on and available at http://www.dynamicdrive.com
* Keep this notice intact for use.

<script>DateInput('orderdate', true, 'DD-MON-YYYY', PassedDate)</script>
</html>Again, how you use this is up to you, as I cannot envision your set up from what you have told me. If you can take care of that part, great, if not, let us know more about how you intend to pass information to this page.

05-09-2005, 03:29 PM
Thanks for that. Sorry to be a pain but I am a complete novice when it comes to programming in java or javascript. The rest of the program code is in php and I have a php variable that I would like to pass to PassedDate. Is this possible?

I am sure I am terrible at explaining but I'll have a go. My php program contains a form which requests various fields, including two dates by using the script. The form has a method of post and an action to call the same program again. The program checks the fields that have been input and updates the mysql database if everything is OK. If not, it gives the form back with the appropriate error messages, leaving the form as it was filled in originally. This is when the date fields are reset and why I would like to be able to pass a date. I have the date I want as a php variable but do not know how to set a variable in javascript.

I hope this makes sense. Thanks so much for your help.


05-09-2005, 04:38 PM
Well, I probably know just about as much about PHP as you do about javascript so, maybe we can both learn something. One thing I do know about PHP packages (programs set up for a particular use that utilize PHP, like PHP List) is that they work with or without javascript. I hope you are aware that using a script for the date field in your form may, depending upon how the form is set up to act in the absence of javascript, make your form a javascript compatible only form. That being said, one way PHP passes variables to pages that I know of is via information included in the url of the page it is sending information to. Like if the page is called newreply.php and the data to be sent to that page is (as is the case, as I type this reply):


the url will be:


Is that the method used to send the date back to your page? If so, we need only know what the prefix for the date data is and the form of the said data. To capture it in javascript and assign it to our variables (we will need two, since you are using two dates) we can use a script like this in the head:

<script type="text/javascript">
var PassedDate1, PassedDate2
if (location.href.indexOf('orderdate=')!==-1)
var PassedDate1=location.href.substr(location.href.indexOf("orderdate=")+10,11)
if (location.href.indexOf('birthdate=')!==-1)
var PassedDate2=location.href.substr(location.href.indexOf("birthdate=")+10,11)

where orderdate= and birthdate= are the prefixes for the passed dates and +10 is the number of characters in the prefix. This will only work if the data for these prefixes is in this form:


If not, we will need to reformat it before using it and, if something other than a date sometimes follows those prefixes in the url, we will have to screen for that. Let's worry about that later, if it is a problem. Now our DateInput lines will look like this:

<script>DateInput('orderdate', true, 'DD-MON-YYYY', PassedDate1)</script>and

<script>DateInput('birthdate', true, 'DD-MON-YYYY', PassedDate2)</script>The fact that I have given the DateInputs the same name as our prefixes is just a coincidence but, since they are the names of the fields, they may well become PHP's prefixes when passing their data. Does this point you in the right direction?

05-10-2005, 03:46 PM
I feel a bit out of my depth here! Php can pass parameters on the URL but also passes any used in the form without attaching them to the URL. I have to use this method as I have no way of rewriting the URL when someone submits their form (I do not know the dates until someone hits the submit button of the form and cannot intercept at this point).

I think I have actually managed to find a way to do it. Php works on the server side and processes code before sending it to the client. I think I can get at the call to java and put the date in. In that case I would only need the one PassedDate parameter, as in your first reply. I will have a go.

I am not sure though what I need to change in the actual java script calendarDateInput.js. Assuming that I can pass the date as in your first reply, please can you tell me exactly what code I have to change in the script.

Thanks so much for your help. I am sure the end users will not appreciate the effort involved when they use it but I sure do!!!

05-10-2005, 06:39 PM
I feel a bit out of my depth here!That makes two of us! Do you have this set up someplace live where I can observe its current behavior? I might be able to figure it out then. I think two variables will still be needed to pass two dates, unless I misunderstood the number of dates involved.
I think I have actually managed to find a way to do it. Php works on the server side and processes code before sending it to the client. I think I can get at the call to java and put the date in. In that case I would only need the one PassedDate parameter, as in your first reply. I will have a go.Let me know.
I am not sure though what I need to change in the actual java script calendarDateInput.js.You shouldn't need to change anything there. What might be helpful to understand is that this:
<script>DateInput('orderdate', true, 'DD-MON-YYYY', PassedDate)</script>is actually a mini script. All you need to do to get the date data passed to the larger script is to get it into the variable 'PassedDate' before the body of the page is parsed by the user's browser. As I said before it needs to be in the format of:


that can be:


as well, and possibly:


will work, not sure about that last one.

One note, there is an important difference between java and javascript. They are two different languages with two different avenues of support in user agents with two different sets of capabilities. I knew you were referring to javascript when you said java in your post but, that can get lost sometimes so, better to use the right word to describe the language you are talking about. No big deal.

05-10-2005, 08:42 PM
Thank you SO much for all your help. It now works! I do have two parameters, one for each date. Everything else is as you said in your first reply - it was just me being thick. As soon as I worked out how to get php to set up the parameters, it was almost easy. I am really grateful for all your help.