954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Check Value of a Drop Down Box without Submitting?

Hi,

I have three drop down boxes for a Date field; one each for days, month and year.

I want it so that when a year and month are selected they can be checked and then i can use those variables to insert the options into the days drop down box. However obviously i do not want to submit the month and year first, so is there a way to check the current selected value of a drop down box?

Thanks.

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

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)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Earlier i found a function to calculate the number of days in a certain month and year on

int cal_days_in_month ( int $calendar , int $month , int $year )


This works fine however i currently have to enter the parameters. I just need to find out a way to insert the month and year selected as parameters without submitting?

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

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)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Never touched js before so i'll have to look over it. Just saw parts on 'OnChange' which could be useful?

I'll try and get my head round it!
Thanks for the help.

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

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)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Okay, i haven't got lots of time to look in depth at js as my project is mainly php so i'll probably dive straight into the jQuery.

Just a few questions to get my head round the code...

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.

What does each of the code snippets do? Don't they both do the same thing?

Where it says e.g. "#days" is that the variable that should be in the select name="" for the checkbox?

Once the code has ran, do i still have to refer to the variable as $("#days") in php or does it go back to normal?

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

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)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Tried the second code snippet and the days drop down box still sppear to be empty?

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

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)
Moderator
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)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 
You have included the jquery library in the head and placed the code inside a document.ready?

Have no idea about what to include?
What do you mean document.ready?

Sorry if these questions may be stupid, i have no experience with this.

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

You're too quick for me - see my last post...

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

I added the code but it still did not work.
Thanks for the help though.

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

Ok i'll have a look when i get home

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 
Ok i'll have a look when i get home

Thanks

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

excuse me, just an off topic question,
Is sir BenZzZ from philippines?

kalaban
Newbie Poster
16 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
 
excuse me, just an off topic question, Is sir BenZzZ from philippines?

No.

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

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)
Moderator
7,800 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Thanks a lot :) that works great.

Just wondering, could you tell me how a few things work in it...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  function getDays(){
	<strong>var days = 32 - new Date($("#year").val(), $("#month").val()-1, 32).getDate();</strong>
   var output = "";
   for(var i = 1; i <= days; i++){
     output += '<option value="' + i + '">' + i + '</option>';
   }
   $("#days").html(output);  
  }
  <strong>getDays();</strong>
  
  
  	$('#year, #month').change(function(){
   		<strong>getDays();</strong>
	});
});
</script>

I can see how most of it works. Just not sure fully of the lines in bold.
I know the first part calculates the number of days, just not sure how?

And i don't understand why the getDays() is called twice?

BenzZz
Junior Poster
116 posts since Feb 2011
Reputation Points: 10
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: