I've got a feeling this may require javascript, but countless google searches haven't turned up anything much useful.
Anyway, here's the description of what I want to do:
I want a form with various lists for people to choose from.
When the user chooses something from the first list, the second list will then be populated based on a MySQL database query.
ie. Say the first list is "category", and the second is "item". When someone chooses an area, I want the list of items to show all the items in that area.
eg. I choose the category "hardware" and the second list shows "motherboard, RAM, Hard Drive" etc.
The catch is that I have lots of items and categories (the example above I just made up to give an idea of what I'm doing), and I don't want to query the database for all items and store them in an array until the user chooses something. I think it would be more efficient (if it's possible) to query the database after the user clicks on the category they want.
Another catch is that the form must look 'the same' (apart from the values in the lists, obviously) the whole time. At the moment I have 'different' pages (all generated by the one big PHP script). One to get the category, then after that is submitted, another page displays the applicable items to choose from.
Anyone have any ideas, or perhaps pointers in the right direction of elsewhere that may be able to help?
Thanks heaps for your time...
Making a form where availible options change based on input
Moderator: General Moderators
Databases?
Well, I want to look up the values in the database AFTER the user clicks on stuff, I have no idea how to do that (or practiaclly anything, for that matter) in javascript.
well, the database lookup you can do with php..
i think you have the misconception that you cannot use both js and php in the same script when in fact you can.
so, do what you need in js, and when you need to query a db, just open up the <?php flag, write teh php code, ?> close it and then do whatever else you need.
i think you have the misconception that you cannot use both js and php in the same script when in fact you can.
so, do what you need in js, and when you need to query a db, just open up the <?php flag, write teh php code, ?> close it and then do whatever else you need.
I realise I can do both js and php (in fact I already am using a js navigation menu), but in php I can only do the database lookup when the page is first generated, right ?
So, AFAIK, I would need to get ALL the info from MySQL via PHP when the page is first generated, then somehow put that info into a javascript which would update the 2nd list when the user clicks on the first ?
I was hoping there was some way to do the database query after the page is generated and the user picks from the list.
Assuming that I have to extract all the data from the database, how would I then put that into a variable in javascript ?
I'm guessing that
would have no way in hell of working.... :\
So, AFAIK, I would need to get ALL the info from MySQL via PHP when the page is first generated, then somehow put that info into a javascript which would update the 2nd list when the user clicks on the first ?
I was hoping there was some way to do the database query after the page is generated and the user picks from the list.
Assuming that I have to extract all the data from the database, how would I then put that into a variable in javascript ?
I'm guessing that
Code: Select all
<script language=javascript>
var all_items = <?php mysql_query("SELECT * FROM table") ?>
</script>I dont know enough about javascript for that part (layers or something), but I am pretty sure that you can use js to dynamically load data on demand from other url's, so you would just add another php script on your server that would serve the requests from the javascript/client and return data only
Thanks Stoker and Infolock,
I ended up just doing some research on javascript, and writing PHP that generated the javascript to update the list boxes.
Well, actually I wrote a js function to do the updating, but PHP generates some js code to store all the values in arrays..
eg:
echo "item[$i][$j][$k] = $item_list[$i][$j][$k]";
I ended up just doing some research on javascript, and writing PHP that generated the javascript to update the list boxes.
Well, actually I wrote a js function to do the updating, but PHP generates some js code to store all the values in arrays..
eg:
echo "item[$i][$j][$k] = $item_list[$i][$j][$k]";