using a MySQL lookup script for a variable selected above

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

using a MySQL lookup script for a variable selected above

Post by Auselan »

I decided to remove this post as it potentially revealed coding i don't want in the public arena
Last edited by Auselan on Tue Dec 11, 2012 2:55 pm, edited 1 time in total.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

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

Post 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.
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

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

Post 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 284 times
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

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

Post 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.
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

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

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

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

Post 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)
Attachments
Capture.JPG
Capture.JPG (17.35 KiB) Viewed 254 times
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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!
Post Reply