SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,037
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Evaluating an XMLHttpRequest response...

    Further in my quest for AJAX dominance, I'm trying to change the position of a second drop down menu based on the selection of a first drop down menu.

    I realize I could use XMLHttpRequest() and a server side request to the database to simply create and replace the second drop down with an identical version indexed at the position determined by the fist drop down, however I thought that there might be a more effective way of doing it with JavaScript.

    I figured I could use XMLHttpRequest to make a call to the database and return a number then evaluate a function based on that. Something like the following:

    Code:
    function changefirm(RemoteAddress) {
            var req = GetXmlHttp(); // fuction to get crossbrowser xmlhttp object
             if (req) {
                req.onreadystatechange = function() {
                      if (req.readyState == 4) { //data is retrieved from server
                           if (req.status == 200) { // which reprents ok status  
                            var fieldvalue = req.statusText;
                            if(fieldvalue != "") {
                                //Change the firm selector control
                                var action = SelectControl(parseInt(req.responseText));
                                eval(action);
                            }
                          } else { 
                            var action = SelectControl('');
                            eval(action);
                          }
                      }            
                  }        
                req.open("GET", RemoteAddress, true); //open url using get method
                req.send(null);
             }        
        }
    The meat & potatoes of the script are here:
    var action = SelectControl(parseInt(req.responseText));
    eval(action);

    SelectControl() is a simple function that takes an integer and moves the drop down to the appropriate value in the list. It works well but I can't figure out how to cast the responseText so that it will evaluate correctly.

    Any thoughts?

    Thanks!
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,037
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Nevermind.... I'm an idiot

    It works just fine but I had left some testing code in another ajax request that was messing with it when they were both called at the same time. DOH

    The code that works is:

    Code:
    var selectid = parseInt(req.responseText);
    var action = SelectControl(selectid);
    eval(action);
    ...And the function that controls the dropdown list is the following:
    Code:
    function SelectControl(ID) {
            var selectcontrol = document.getElementById('firm');
            selectcontrol.value = ID;
     }
    ...Where firm is the name/id of the select list of firms.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •