Forum Moderators: open

Message Too Old, No Replies

ajax for drop down boxes

         

ksugam

6:24 pm on Feb 22, 2008 (gmt 0)

10+ Year Member



Hello all,
I have a PHP GET on a txtbox which takes input as yyyy/mm. Now i am thinking of changing this txt box into 2 drop down boxes, one for year and other for month.

Also, I want to populate the month box depending on what year is selected in the year box...

say for e.g., if user selects 2008 from year drop down, the month drop down should just have months jan and feb...
if user selects 2007 in the year drop down,thn month drop down will be jan to dec....
How to do this?

Looking forward to ur reply!

le_gber

4:42 pm on Feb 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try Googling for 'dynamic dropdown boxes' It's all done in javascript and using onchange on the first drop down to call the function that populate the second drop down..

ksugam

1:53 am on Feb 26, 2008 (gmt 0)

10+ Year Member



thanks!
I have implemented it....now my concern is, how to retain the values selected in the drop down box?

le_gber

8:30 am on Feb 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you mean by retain the value?

ksugam

5:20 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



so i have 2 dropdown boxes, year and month. say if i select 2007 and jan and press submit.
I want the boxes to display 2007 and jan after it loads the page....

I hope this explains....sorry for not explaining in detail earlier...

eelixduppy

5:34 pm on Feb 26, 2008 (gmt 0)



You are going to have to use the $_POST superglobals that were submitted to the action page to repopulate the select drop down appropriately. So when you are going through the list of elements, you have to compare that value with the one submitted and echo 'selected' accordingly.

ksugam

5:42 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



can you show me an example for the explanation.....you dont have to explain the basics(as i m aware of post and superglobals)...just an example...
sorry about tht....

eelixduppy

5:45 pm on Feb 26, 2008 (gmt 0)



[webmasterworld.com...]

While this solution is from data populated from a database, the method still applies here.

ksugam

6:38 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



so i have a drop down as follows:

<FORM action="<? echo $_SERVER['PHP_SELF']; ?>" method="GET">
<SELECT NAME="year">
<Option value="2007">2007</option>
<Option value="2008">2008</option>
</SELECT>
<input type="submit" name="Go" value="Go">
</FORM>

I also have $_SESSION['year'] and $_GET['year']...

The first time user goes to this page, the year is taken from the SESSION variable, after tht it is taken from GET...

How do i implement it in here?

ksugam

7:33 pm on Feb 26, 2008 (gmt 0)

10+ Year Member



i got the solution:

i was looking for this:

<SELECT NAME="year">
<Option value="2007" <? if((isset($_GET['year'])?$_GET['year']:$_SESSION['year'])==2007) echo "selected" ?>>2007</option>
<Option value="2008" <? if((isset($_GET['year'])?$_GET['year']:$_SESSION['year'])==2008) echo "selected" ?>>2008</option>
</SELECT>

eelixduppy

9:30 pm on Feb 26, 2008 (gmt 0)



Looks good; glad you figured it out :)

ksugam

6:11 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



one more query on similar lines:
I have 2 dynamic drop down boxes: year and month. I am using a javascript function to populate the month box depending on the option selseted in the year box.. I am using addOption to [populate the boxes...
So similarly i was wondering how can i retain the values on these boxes as i did it for the year drop down on other pages....

eelixduppy

7:23 pm on Feb 27, 2008 (gmt 0)



If you are submitting this form to another page then you can use the same method for retaining the month. You are going to have to use javascript, though, to retain this value because you are using javascript to populate the second dropdown based on the year selected. Therefore you are going to have to echo the value of the month selected into a javscript variable and then writeln the appropriate "selected" for the month actually selected with javascript this time, instead of php. Unless, of course, you want to populate the month dropdown on the action page with php, then you can bypass this step.

If you are going to be needing these values over and over again you might just want to consider using cookies and storing them that way. Then you know where to get them when you need that data and if you need to make an update, you just update the cookie directly. Not to mention cookies and accessible through php and javascript, so it should work out well if you decide to take that implementation.