PDA

View Full Version : Questions for the web designers/programmers



nofam
28-05-2009, 11:51 AM
Can anyone give me (or point me to) a quick overview of how to build dependent dropdown lists in a PHP form? i.e.

You have 2 separate lists, called Food and Type

Food is populated with Fruit & Vegetables

Type has all the following options:

Apples
Bananas
Carrots
Cauliflower
Pear
Pineapple
Turnip

If you picked Fruit you would only be able to select Apples, Bananas, Pear or Pineapple, if you picked Vegetables, only Carrots, Cauliflower and Turnip would appear.

I'm a little unsure how to proceed, as my (VERY limited) understanding is that PHP is stateless, and the first list can't pass on the selection to second until the form is submitted.

I've seen reference to a piece of javascript called Onclick; would this help?

:waughh:

:thumbs:

nofam
02-06-2009, 12:18 PM
Bump?

:crying

Bozo
02-06-2009, 01:50 PM
Ok - 2 ways you can do this:

1 - Re-Databind the dropdownlist and have it use a different datasource depending on the selection (fruit/veges/all). Use a Javascript method to call a function in the code behind - not too sure how it goes about pagepostbacks and partial pagepostbacks etc in PhP though.

2 - Use several different dropdownlists.
DropdownlistFruit / DropdownlistVeges / DropdownlistAll
You could hide/show the different dropdowns based on the users selected view type.
Not too sure with PhP but you might be able to associate the Visible property of the different DropdownLists with the selected View Type:

(Asp.net example - sorry don't know PhP very well)

DropDownListFruit.Visible='<% #CheckBoxFruit.Checked %>'

Or if this doesn't work, you should be able to use a JavaScript Method to call a method in the code behind the page to set the Visible state of the dropdownlists to true or false.

I know in ASP I wrap these sort of controls that need to be updated without a full pagepostback in an Ajax UpdatePanel - not sure what the PhP equivalent would be though sorry.

My thoughts anyway - if anyone with more PhP knowledge would like to expand on this I would love to see how it is done.

somebody
02-06-2009, 02:00 PM
Can anyone give me (or point me to) a quick overview of how to build dependent dropdown lists in a PHP form? i.e.

You have 2 separate lists, called Food and Type

Food is populated with Fruit & Vegetables

Type has all the following options:

Apples
Bananas
Carrots
Cauliflower
Pear
Pineapple
Turnip

If you picked Fruit you would only be able to select Apples, Bananas, Pear or Pineapple, if you picked Vegetables, only Carrots, Cauliflower and Turnip would appear.

I'm a little unsure how to proceed, as my (VERY limited) understanding is that PHP is stateless, and the first list can't pass on the selection to second until the form is submitted.

I've seen reference to a piece of javascript called Onclick; would this help?

:waughh:

:thumbs:

You would be best doing this client-side using Javascript. Try searching for what's called "cascading drop down lists" - assuming you want to achieve something similar to this: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/CascadingDropDown/CascadingDropDown.aspx

Erayd
02-06-2009, 02:14 PM
Ok - 2 ways you can do this:

1 - Re-Databind the dropdownlist and have it use a different datasource depending on the selection (fruit/veges/all). Use a Javascript method to call a function in the code behind - not too sure how it goes about pagepostbacks and partial pagepostbacks etc in PhP though.

2 - Use several different dropdownlists.
DropdownlistFruit / DropdownlistVeges / DropdownlistAll
You could hide/show the different dropdowns based on the users selected view type.
Not too sure with PhP but you might be able to associate the Visible property of the different DropdownLists with the selected View Type:

(Asp.net example - sorry don't know PhP very well)

DropDownListFruit.Visible='<% #CheckBoxFruit.Checked %>'

Or if this doesn't work, you should be able to use a JavaScript Method to call a method in the code behind the page to set the Visible state of the dropdownlists to true or false.

I know in ASP I wrap these sort of controls that need to be updated without a full pagepostback in an Ajax UpdatePanel - not sure what the PhP equivalent would be though sorry.

My thoughts anyway - if anyone with more PhP knowledge would like to expand on this I would love to see how it is done.
You don't actually do this in PHP at all, unless you're doing it via a third-party framework that generates the necessary javascript. Javascript does the real work here.

Bozo
02-06-2009, 02:26 PM
You don't actually do this in PHP at all, unless you're doing it via a third-party framework that generates the necessary javascript. Javascript does the real work here.
Ok sweet as - yea I have hardly used PHP at all - just thinking out-loud what I would do if it was ASP.NET and was wondering if PhP had something similar or not.

nofam
02-06-2009, 03:58 PM
Cool - thanks guys. . .Gives me something to go on at least.

Slightly OT, but how's the new job going DH?

Maybe you should start a new thread to give inquiring minds an update!! :D