Re: form & lookup columns [message #172364 is a reply to message #172353] |
Mon, 14 February 2011 12:02 |
Tim Streater
Messages: 328 Registered: September 2010
Karma:
|
Senior Member |
|
|
In article <ijaugn$7at$1(at)news(dot)albasani(dot)net>,
The Natural Philosopher <tnp(at)invalid(dot)invalid> wrote:
> Wolf wrote:
>> Hi,
>>
>> I've got a database with these tables:
>>
>> Region (id, name)
>> Province (id, id_region, name, shortname)
>> Town (id, id_province, name)
>> Shop (id, id_town, name...)
>>
>> I've made a form and I'd like that when I choose a region, in the next
>> drop-down menu I want to show provinces of that region
>>
>> I've searched the web to study how to do this thing, but I haven't
>> found anything: could you please give me some hints/ideas/links to
>> tutorials?
>
> If you want that to hapen without submitting the page back to the
> server, you have two possibilitioes. One is a partial resubmit using
> IIRC Iframes, the other is to downlaod the whole bloomin lot and use
> javascript - and a lot of it - to dynamically change what appears on
> one box as a result of selecting in another, without reference to the
> server..
>
> This means effectively replicating a lot of what SQL does in javascript
> on data held in javascript arrays.
>
> It is not trivial.
That was my first approach when faced with the problem. Doesn't scale as
the amount of JS (and I was generating it using PHP) goes up as the
product of the number of selects.
> I am not clueful on iframes, but these may represent a simpler approach.
That was my second approach. I constrained the iframe's size by putting
it in a table cell (and there may well be better ways of doing *that*),
connected an onselect handler to the first select, that caused a reload
of the iFrame based on what had been selected. I still had to generate
the whole page for the iframe using PHP, including, obviously, onselect
handlers for the next select. You need one iframe for each select except
the first. This approach works better, but still transfers more data
each time than you'd like.
In the end I learnt about ajax, and used that. The onselect handler
makes an ajax request, and the PHP script returns the appropriate data
so the adjacent select can be loaded by the callback function you
specify in your ajax call. No iframes, and a lot less data transferred.
I'm assuming, here, that you've got some method of putting data into
your database which is separate from what we're talking about here, and
so you need a method that works independently of the amounts of data in
each table. So when the page is loaded, it should use ajax to load up
the first select (regions). When the user chooses one, you load the
second select (province). And so on.
But note this: if the user chooses a region, then a province, and then a
town, you've got three selects populated. If now the user goes back and
chooses a new region, you'll have to dump the data in the second and
third selects (province, town) and repopulate the second select
(province).
--
Tim
"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
|
|
|