[Webdev] PHP+Javascript request

Dave Wilson dave.wilson at heanet.ie
Mon Aug 19 18:10:31 IST 2002


It's like this:

I have a form. It is generated by some PHP. It contains two items. One 
is a SELECT MULTIPLE list of sites. The list of elements (sites) is held 
in a PHP array. (imagine Cork, Galway, Dublin, Kerry, etc).

The other item is a SELECT (not multiple) drop down box with a list of 
regions. (Imagine Connaught, Leinster, Munster, etc). I don't mind how 
they're stored.

Each region is related to one or more sites. I'd like the user to be 
able to choose a region from the drop down box, and have it 
automatically select the relevant elements from the SELECT MULTIPLE. 
(Imagine selecting "Munster" highlights Cork and Kerry).

This is easy to do in isolation; dump the region->site mapping in a 
Javascript array, have an onClick (or whatever) on the SELECT which 
calls a function; the function highlights the necessary elements in the 
SELECT MULTIPLE. However.

In order for PHP to parse the results of the SELECT MULTIPLE after it's 
submitted, the variable name needs to end with []. That's great; PHP 
will take each selected element and plant them as consecutive elements 
in an array. But that makes for quite messy code when trying to link the 
regions SELECT action with the sites SELECT MULTIPLE. Javascript can't 
refer to the SELECT MULTIPLE by name because of the brackets, so I have 
to refer to it by its location in the form.

So I can get it working, but if I change it, it all goes to hell very 
quickly. And I'd like to be able to add arbitrary sites and regions 
without rewriting the code. That is, I'd like an array or database that 
says "Leinster means Dublin,Kildare; Connaught means Galway; Muster 
means Cork,Kerry" and gets that info into the code without getting 
bogged down in making me calculate array indices. Does anyone have some 
code that will do this?

If the description is clear as mud, let me know and I'll plant up a page 
with the working model - it might ring a few bells.

Dave





More information about the Webdev mailing list