Portal Home > Knowledgebase > Articles Database > Need help with Javascript Form


Need help with Javascript Form




Posted by pwalters09, 01-12-2010, 03:54 PM
Hi guys I am having a little trouble, I need a form that automatically updates when another dropdown box option is selected For example: I have a menu item with a list of categories, a user clicks on the movies category and the subcategory list automatically populates with the subcategories for movies Or: A user clicks on the software category, and the subcategory list automatically populates with the subcategories for software And so on... The categorys and subcategories are in a database, for example the main category for movies is ID number 10003, and the code i use in the subcategories dropdown for that specific main category is That code simply fetches the subcategorys for the specific main category, of course i have lots of categorys which are fetched from the database, but i want to be able to automatically update the subcategory dropdown with the correct subcategories only for the main category. A perfect example would be demonoid.com's torrent search feature Thanks...

Posted by mattle, 01-12-2010, 05:26 PM
There are two basic ways to accomplish this: The old-school way: Set up an onChange() event when the main category runs that reloads the page with a GET URI (http:...php?main_cat=10003) Then in your PHP script if $_GET['main_cat'] is set, you draw the second select box with just the right options.The new-school way: Set up an onChange() event that fires an AJAX routine. This routine passes (probably via GET) the main category to a short PHP script. The return value of that PHP script is then put between the beginning and end select tags for your subcategory. The short PHP script simply reads the main category id and echoes the appropriate option list. There's a whole ton of tutorials on both of these approaches that are just a google away

Posted by pwalters09, 01-12-2010, 07:12 PM
OK lol sorry for the hassle, after a little twidling with my script i managed to get this far, And here is an example of one of the PHP files I call to get the list of subcategories Now the search form will update the forms as I wanted it to, and auto populates the second dropdown, now my question is how can I set an option value, see my database goes by ID numbers for the subcategories, but when I search it comes up with the main subcategory name, i need it to display the ID in the option which is passed to the server, but i need the users to see the subcat name, at the minute it looks like this: But I need it like this: Any help doing this? Thanks again

Posted by mattle, 01-13-2010, 09:26 AM
Sorry...I don't have the time to look through all that code you posted. Generally, when posting code, you should post the minimum amount of code necessary to replicate the behavior you're experiencing. Also, please use the [PHP] tags to take advantage of syntax highlighting and preserve your indentation...makes it a lot easier for us to get an idea of what's going on at a glance.

Posted by pwalters09, 01-13-2010, 09:40 AM
its not a problem mate i just wrote a little script to select the id of the subcategory based on the main category and sub category name Thanks anyways



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
DNS query question (Views: 626)
Raid 1 to Raid 10 (Views: 583)