You can't use a datepicker?
You'll need to use js to do this if you don't want a submit on each dropdown selection.
There's a function here: http://snippets.dzone.com/posts/show/2099
that may be of help:
function daysInMonth(iMonth, iYear){
return 32 - new Date(iYear, iMonth, 32).getDate();
}
So, I suggest you only have real values in YEAR and MONTH fields as you don't want to provoke an error. So whenever these fields are selected/changed they will run daysInMonth, which will return the number of days. This can be modified if you're using jQuery:
function daysInMonth(){
var days = 33 - new Date($("#year").val(), $("#month").val(), 32).getDate();
for(var i = 1; i < days; i++){
output += '<option value="' + i + '">' + i + '</option>';
}
$("#days").html(output);
}
THis is not tested and I'm forever getting my js knickers in a twist.
//EDIT
Obviously you'll need to set up .change handlers for the month and year fields:
$('#year, #month').change(function(){
var days = 33 - new Date($("#year").val(), $("#month").val(), 32).getDate();
for(var i = 1; i < days; i++){
output += '<option value="' + i + '">' + i + '</option>';
}
$("#days").html(output);
});
You need to place this within a document.ready script or place at the bottom of the page.
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
This is php not js. Equally you could use:
date('t', mktime(0, 0, 0, $m, 1, $y));
If you want php, use ajax, but it involves a round trip to the server just to find the number of days. You'll still have to use js. ;)
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
My js code uses jQuery .change method which is analogous to the OnChange method in vanilla javascript. If you're starting out in js, it's an idea to get a feel for normal js before getting into jQuery, but there again, if you're just using js for the trivial bits of functionality, you could just dive into it. As you are reasonably constrained by the structure and there's so much support out there, you could build functions quite quickly.
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
They do the same thing, but if you just leave it as the first example, you'll need to set up an OnChange attribute in both month and year dropdowns - bit messy.
The #days, #month, #year references mean that they are looking for:
...
...
...
PHP is NOT involved.
If you submit the form once you've selected the date in full, you just pick up the data from $_POST['days'],.. etc.
Of course you could run an ajax call to php every time that the month or year was changed, but that's a bit extreme IMO.
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Well, as I said, I'm no pro with JS - however, I don't think it's far off.
You have included the jquery library in the head and placed the code inside a document.ready?
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#year, #month').change(function(){
var days = 33 - new Date($("#year").val(), $("#month").val(), 32).getDate();
for(var i = 1; i < days; i++){
output += '<option value="' + i + '">' + i + '</option>';
}
$("#days").html(output);
});
});
</script>
</head>
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
You're too quick for me - see my last post...
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Ok i'll have a look when i get home
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
OK, had a look - no wonder you couldn't get it to work! Had a 'few' errors:
Try
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
function getDays(){
var days = 32 - new Date($("#year").val(), $("#month").val()-1, 32).getDate();
var output = "";
for(var i = 1; i <= days; i++){
output += '<option value="' + i + '">' + i + '</option>';
}
$("#days").html(output);
}
getDays();
$('#year, #month').change(function(){
getDays();
});
});
</script>
which the following:
<form>
<select id="days">
</select>
<select id="month">
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>
<select id="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>
</form>
You can produce your years (and months for localization purposes) dynamically from php if req'd. Can't help thinking that this should have gone in the js forum though. :)
diafol
Rhod Gilbert Fan (ardav)
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080