Page 1 of 1

using a MySQL lookup script for a variable selected above

Posted: Thu Jan 01, 2009 2:52 pm
by Auselan
I decided to remove this post as it potentially revealed coding i don't want in the public arena

Re: using a MySQL lookup script for a variable selected above

Posted: Thu Jan 01, 2009 3:05 pm
by watson516
You have to give the option tags a value.

Code: Select all

...
<select name="practice">
     <option value="Value">Display</option>
</select>
...
If you're going to put the value of whatever the user selects into a mysql statement, you need to check the value to make sure it is a valid one when the user submits the form. If you don't, you offer the user an opportunity to cause havoc.

Re: using a MySQL lookup script for a variable selected above

Posted: Thu Jan 01, 2009 5:07 pm
by Auselan
When the user selects the practice it will be by dropdown box html rather like this:

Code: Select all

<select name='practice'>
<option>Bartlemas Surgery</option>
<option>27 Beaumont Street</option>
<option>*all the other practices echo'd by the php script*</option>
</select>
which will ensure they select only practices that are in the database. What I'm hoping is to do is find a way to make the second box limit its MySQL query to the first "Practice" dropdown box. The second box will appear like this

Code: Select all

<select name='contact'>
<option>Mrs Miggins</option>
<option>Dr wotshisface</option>
<option>*all the other contacts who generated by the MySQL query *</option>
</select>
Am I making any sense? I've linked a screenshot of the form in action - I want the list which I've dropped down to bring up only those linked to the selected practice (in this example, 27 Beaumont Street)
Untitled.jpg
Untitled.jpg (25.37 KiB) Viewed 285 times

Re: using a MySQL lookup script for a variable selected above

Posted: Thu Jan 01, 2009 5:21 pm
by watson516
You should still check the data even though the data is in a select. You should also add the value unless you're not doing anything using that value.

If you want to change the second select box depending whats selected in the first, you need to use javascript. You could either populate the values within the second select box or you can load a bunch of select and only show the one that is required. I am not sure which is the better way as I don't really know javascript. Good luck.

Re: using a MySQL lookup script for a variable selected above

Posted: Fri Jan 02, 2009 6:19 pm
by Auselan
I found a workaround which, while not neat ,does the job - have the first function in a small page on its own - a lookup box that uses the selection to feed the data via a POST command to a second page which makes use of it

Re: using a MySQL lookup script for a variable selected above

Posted: Fri Jan 02, 2009 7:59 pm
by califdon
The basic issue is that you can't do this all in PHP, because at the time PHP is creating the HTML for the second list box, it hasn't even been sent to the browser, so there's no way for it to know what selection the user will make from the first list box. Well, you could do it all in PHP if you used a complete page refresh, but that's ugly. It will take some thought and programming, but you can do it either in pure Javascript or by using Ajax, which is a combination of Javascript in the browser and PHP on the server.

I agree with you that there is no need to check the validity of the user selections, especially if you use Javascript or Ajax. You control what values can be introduced.

Re: using a MySQL lookup script for a variable selected above

Posted: Sat Jan 03, 2009 7:14 am
by Auselan
Thanks Jack

While it is hardly fancy programming, I went for the lesser of two weavels and just split the page into two, with the first feeding into the second (see attached image)

Re: using a MySQL lookup script for a variable selected above

Posted: Sat Jan 03, 2009 1:03 pm
by califdon
Auselan wrote:While it is hardly fancy programming, I went for the lesser of two weavels and just split the page into two, with the first feeding into the second (see attached image)
This can often be a very practical way to handle a "serial choices" requirement without the need to rely on Javascript (since a few viewers disable JS in their browsers--about 5% according to one survey I've seen). If it is well designed, it can yield a perfectly fine interface. I, for one, don't believe in fancy programming just for its own sake. If a simple approach achieves your purposes, use it!