Forum Moderators: open

Message Too Old, No Replies

Ajax to Display Results from 2 Radio Sets

         

primasilva

6:30 pm on Mar 7, 2010 (gmt 0)

10+ Year Member


Alrighty, my page has two sets of radio buttons. I want the user to click one from one set, then another from another set, resulting in a list of items that are shared between the two sets. I can get each set to work separately, but they're not cooperating together. Wow, that sounds confusing. Here's some of the code:

//Set 1 (radio button 1 displays an array of items a, b, c; rb2 displays d, e, f):
...onClick="ajaxHandler2(this.value)"...

//Set 2(rb3 displays a, b, d, f; rb4 does b, c, d, e)
...onClick="ajaxHandler2(this.value)"...


So, for example, if the user selects rb1 and rb3, what I want displayed in my div is :
a
b

//the important part of my ajax handlers. This is where I think I'm messing up. Never used ajax before...
function ajaxHandler(arg)
{
xhr = new XMLHttpRequest();
xhr.onreadystatechange = readyStateHandler;
xhr.open("GET", "myServlet?firstChoice" + "=" + arg, true);
xhr.send(null);
}

function ajaxHandler2(arg)
{
xhr = new XMLHttpRequest();
xhr.onreadystatechange = readyStateHandler;
xhr.open("GET", "myServlet?secondChoice" + "=" + arg, true);
xhr.send(null);
}

function readyStateHandler()
{
if (xhr.readyState==4)
{
document.getElementById("placeholder").innerHTML = xhr.responseText;
}
}


//part of my servlet, where I could also be messing up
for(int k=0; k<firstArray.length; k++)
{
for(int m=0; m<secondArray.length; m++)
{
if(firstArray[k].equals(secondArray[m]))
{
out.println(firstArray[k]);
out.println("<br/>");
}
}
}

daveVk

2:34 am on Mar 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you need only one ajax handler

function ajaxHandler(){
xhr = new XMLHttpRequest();
xhr.onreadystatechange = readyStateHandler;
xhr.open("GET", "myServlet?firstChoice=" + ..codeToGetValue1.. + "&secondChoice=" + ..codeToGetValue2.., true);
xhr.send(null);
}

To be called on any change

primasilva

4:04 am on Mar 8, 2010 (gmt 0)

10+ Year Member



Awesome, that worked! Thanks for your help!