0

I have one JSP page which consists of three forms and each form consist a drop downlist.

On the first drop downlist onchange event is called and gets data from the database and save the data into another dropdown list. My problem is when the page reloads the data choosen in the dropdownlist is changed.

How can I solve the problem that the data is not changed when the page load?

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
Amit
  • 497
  • 3
  • 8
  • 24
  • possible duplicate of [Populating child dropdownlists in JSP/Servlet](http://stackoverflow.com/questions/2263996/populating-child-dropdownlists-in-jsp-servlet) – BalusC Mar 03 '11 at 14:10

3 Answers3

1

AJAX part is here:

Enclose your contents in 3 separate divs. Now, call reloadReqdForm() function, instead of submitting the page. something.do is the name of Action you are using. Now, whatever will be the result it will come as req.responseText in the last function, which u will put in message var. div1 is the e name/id of the div that u want to reload.

function reloadReqdForm(){

var url = "something.do?queryString";
       if (window.XMLHttpRequest) {
           req = new XMLHttpRequest();
       } else if (window.ActiveXObject) {
          req = new ActiveXObject("Microsoft.XMLHTTP");
        }
       req.open("POST", url, true);
       req.onreadystatechange = callbackReloadForm;
       req.send(null);
}
function callbackReloadForm()
 {
 if (req.readyState == 4) 
 {
    if (req.status == 200) {
        drawResponse();
      }
 }
}

function drawResponse() {
var message = req.responseText;
     document.getElementById("div1").innerHTML=message;
}

Hope, it will work.

Akshatha
  • 599
  • 2
  • 10
  • 26
Raman
  • 1,507
  • 3
  • 15
  • 25
0

How about using JQuery One method which makes the handler execute only once.

$('something').one('onChange', function(e) {
alert('You will only see this once.');
});
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
0

one solution is use AJAX. Another is submit the values of dropdown that u want to preserve & get those vales back from controller by setting them in model object.

Raman
  • 1,507
  • 3
  • 15
  • 25
  • can u provide the required ajax code,actually i am working on struts 1.0 so the data came through the action class of struts – Amit Mar 03 '11 at 11:33
  • If u want to do without using AJAX, then in Struts, u can set attribute in ActionForm. Set that attribute in ur Action class and then access that attribute of ActionForm bean in JSP to populate values in dropdown. If u want to do it wid AJAX, here it goes – Raman Mar 03 '11 at 13:49
  • I am adding it as another answer, due to space issues. – Raman Mar 03 '11 at 13:56