File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Ajax drop down Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax drop down" Watch "Ajax drop down" New topic
Author

Ajax drop down

chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hello friends ,

interesting question to all experts out there, I have one drop down and when user clicks i need to get data using ajax with java classes. but how to load data using ajax and place that using specific format.

the structure is using Ajax, JSON and JAvascript

- any help is much appreciated
- thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

What do you have coded so far?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jason Ferguson
Ranch Hand

Joined: Sep 16, 2007
Posts: 47
Originally posted by chintan ramavat:
Hello friends ,

interesting question to all experts out there, I have one drop down and when user clicks i need to get data using ajax with java classes. but how to load data using ajax and place that using specific format.

the structure is using Ajax, JSON and JAvascript

- any help is much appreciated
- thanks


Chintan,

Are you attempting to populate the contents of a second <SELECT> element based on input from the first? This is commonly referred to as a chained selector.

You cannot do this with only Javascript (note: AJAX and JSON are not programming languages. AJAX refers to a methodology, and JSON is a data format in the same way as XML is). You need a Java class to do this on the server side. I tend to use a struts action to do it. You may also wish to take a look at json-lib (http://json-lib.sourceforge.net), which converts Collections, Maps, and individual objects to JSON.

I also have a chained selector javascript function, which relies on the Prototype javascript library. Let me know if you are interested.

Jason
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Jason Ferguson:
You need a Java class to do this on the server side.
Not necessarily. Firstly, we're not even sure that dependent drop-downs are what the OP is referring to. Secondly, if the data set is small and relatively semantic-free, its perfectly possible to handle this with a series of static text files on the server. Moreover, even if server-side assistance is required, there are more choices than just Java (php, cgi, etc).

The OP does specify Java, but for others that might be wanting to set up similar mechanisms, Java is not the only choice.
[ February 24, 2008: Message edited by: Bear Bibeault ]
Kalai Selvi
Ranch Hand

Joined: Jan 08, 2007
Posts: 45
Hello Jason,
I used Prototype.js to build this chained selector. My flow is basically like this:

Initially in my jsp only one drop down will have values and the other will be disabled. Onchange of first dropdown i do an ajax call which calls my servlet. The servlet queries the database based on the parameter sent to it
and returns a delimited string as response.

Again, back in responsetext in ajax, i use string.split method to convert that delimited string to javascript array and populate the options of the second dropdown.

Before that, i remove all the existing options in the second dropdown before populating the fresh data.

This is working fine. But when i test it by changing the selection in the first dropdown, after say, 4 or 5 selections, the application slows down and is hanging. It says, a script is causing it to run slowly. please stop running the script.

What could be the reason? Here is the javascript function which gets the response from the servlet

Note :grpElement is the name of my second drop down list

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Kalai Selvi:
It says, a script is causing it to run slowly. please stop running the script.
This usually means that your script has entered an infinite loop.

Carefully reconsider the following code:
Kalai Selvi
Ranch Hand

Joined: Jan 08, 2007
Posts: 45
Thanks for the help.

Actually that piece of code was getting into an infinite loop.
I changed my code to



It is working fine now.


Kalai Selvi
Pooja SKumar
Greenhorn

Joined: Jul 21, 2008
Posts: 1
Hi Everyone,
Even I have the same question i.e,
I have 2 drop down boxes and this drop down will have values and the other will be disabled. Onchange of first dropdown i need to do an ajax call which calls my servlet. The servlet queries the database based on the parameter sent to it and returns values which become the values of the 2nd drop down box.I need the complete working code for this.
I dont want structs,I need simple code involving JSP,Java Scripts,Ajax and servlets.PLease help me.
This is a critical requirement which I'm trying to solve form the past 2 months.I got php with Ajax code with this,but I dont want that...
I need Ajax with servlets.
Please reply and help me as soon as pujaa26@yahoo.co.in possible.
My Email Id pujaa26@yahoo.co.in
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

[Edit: thread hijack removed. Please start your own questions in a new topic]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax drop down