views:

42

answers:

2

Example need for ajax, where on selecting a radio button will dynamically produce drop down

A: 

could you explain yourself a little more?

You need an example where if you click on a radio button and select is showing?

if yes you don't need ajax.

My radio: <input type="radio" name="YOUR_RADIO" onclick="document.getElementById('selectfield').style.display='';" />

<div id="selectfield" style="display:none">
<select><option>option</option></select>
</div>

good luck ...

Mihai Iorga
+2  A: 

If you use or can use jQuery, this is the way I would do it:

Having this HTML:

<input type="radio" name="myradio" value="foo" />
<input type="radio" name="myradio" value="bar" />
<input type="radio" name="myradio" value="baz" />

and assuming you have a page "your_page.php" that returns the list to populate the drop down as JSON, do this:

$("input[@name='myradio']").change(function(){
    var selected_value = $("input[@name='myradio']:checked").val();
    $.getJSON("your_page.php", { value: selected_value }, populate_dropdown);
});

function populate_dropdown(items) {
    // "items" is the ajax-loaded list based on the selected radio button.
    // Clear the drop down, populate it and show it if hidden.
}
Dom De Felice
Ah. `$` how we love you so. Well. People writing libraries love it anyway. If you're going to depend on a third party library then it is worth mentioning which one — especially with as an opaque name as `$` which is used by Prototype and Moo Tools (among others)
David Dorward
Oops.. I would swear I had read the jquery tag! Really sorry. I updated my answer.
Dom De Felice
@David - On site named jQueryOverflow, it's obviously jQuery! (Well, the site should be named that...)
Coronatus
Looking better. I'll give you an upvote if you use the *data* argument of the method instead of the (deprecated) escape function. :)
David Dorward
Done, thanks for the suggestion :)
Dom De Felice
Looking rather good now.
David Dorward