Hi,

The XML can be in whatever form you find useful to parse. Here's a
complete example that handles an XML response that looks like this:

<states><state code="CA">California</state><state code="AK">Arkansas</
state></states>

Code (also -- more readably -- here: http://pastie.org/1097106):
* * * *
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
#log p {
    margin:     0;
    padding:    0;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/
libs/prototype/1.6.1.0/prototype.js'></script>
<script type='text/javascript'>
document.observe('dom:loaded', function() {
    $('btnGo').observe('click', go);

    function go() {
        new Ajax.Request("getlist.jsp", {
            onSuccess:  getlistSuccess,
            onFailure:  getlistFailure,
            on0:        getlistFailure
        });
    }

    function getlistSuccess(response) {
        var states,
            state,
            selbox,
            options;

        // Validate response
        states = response.responseXML &&
response.responseXML.firstChild;
        if (!states ||
            !states.tagName ||
            states.tagName != "states") {
            getlistFailure(response);
            return;
        }

        // Get the select box and its options "array" (not really an
array)
        selbox = $('selectbox');
        options = selbox.options;

        // Loop the states
        for (state = states.firstChild; state; state =
state.nextSibling) {
            options[selbox.options.length] = new Option(
                state.firstChild.nodeValue,
                state.getAttribute("code")
            );
        }
    }

    function getlistFailure(response) {
        log("getlist call failed");
    }

    var write = log;
    function log(msg) {
        $('log').appendChild(new Element('p').update(msg));
    }
});
</script>
</head>
<body>
<input type='button' id='btnGo' value='Go'>
<br><select id='selectbox'></select>
<hr>
<div id='log'></div>
</body>
</html>
* * * *

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

On Aug 16, 10:00 pm, Phil Petree <phil.pet...@gmail.com> wrote:
> I've looked everywhere for a solution to this and hacked around with it all
> day with no luck.
>
> How do we populate a select box using xml data obtained from an ajax
> request.
>
> If what I get back from the fillin example is this:
> <data>
> <first>Jack</first>
> <last>Flash</last>
> <email>jfl...@candlestick.com</email>
> </data>
>
> And I want to add a list of US states to a select box but obviously we can't
> have
>
> <state>AK</state>
> <state>AL</state>
>
> So question 1 is: What's the correct format for the XML response?
>
> My 2nd question is how do I break out the seperate parts from the transport
> response?
> transport.responseXML.getElementsByTagName('$fieldname')[0].firstChild.node 
> Value;
>
>  My 3rd question is, will the following code work for populating the select
> box?
>                 opt.text = "Alaska";
>                 opt.value = AK;
>                 $("DropDownStateList").options.add(opt);        // Assign
> text and value to Option object

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to