Jump to content

XMLHTTP Help


Canman2005

Recommended Posts

Hi all

 

Wonder if someone can help

 

I have the following code

 

// declare a global  XMLHTTP Request object

var XmlHttpObj;

function CreateXmlHttpObj()

{
     
    try
    
    {
        
        XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
        
    }
    
    catch(e)
    
    {
        
        try
        
        {
            
            XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
            
        }
        
        catch(oc)
        
        {
            
            XmlHttpObj = null;
            
        }
        
    }
      
    if(!XmlHttpObj && typeof XMLHttpRequest != "undefined")
    
    {
        
        XmlHttpObj = new XMLHttpRequest();
        
    }
    
}

function ListOnChange()

{
    
    var county = document.getElementById("county");
    
   
    var selectedContinent = county.options[county.selectedIndex].value;
    
  
    var requestUrl;
  
    requestUrl = "code/xml.php" + "?filter=" + encodeURIComponent(selectedContinent);
        
    CreateXmlHttpObj();
    
  
    if(XmlHttpObj)
    
    {
              
        XmlHttpObj.onreadystatechange = StateChangeHandler;
    
        XmlHttpObj.open("GET", requestUrl,  true);
                
        XmlHttpObj.send(null);
        
    }
}

function StateChangeHandler()

{
       
    if(XmlHttpObj.readyState == 4)
    
    {
        
        if(XmlHttpObj.status == 200)
        
        {
            
            PopulateCountyList(XmlHttpObj.responseXML.documentElement);
            
        }
        
        else
        
        {
            
            alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
            
        }
        
    }
    
}

function PopulateCountyList(countyNode)

{
    
    var region = document.getElementById("region");
    
    for (var count = region.options.length-1; count >-1; count--)
    
    {
        
        region.options[count] = null;
        
    }
    
    var countyNodes = countyNode.getElementsByTagName('county');
    var idValue;
    var textValue;
    var optionItem;
    
    for (var count = 0; count < countyNodes.length; count++)
    
    {
        textValue = GetInnerText(countyNodes[count]);
        idValue = countyNodes[count].getAttribute("id");
        optionItem = new Option( textValue, textValue,  false, false);
        region.options[region.length] = optionItem;
        
    }
    
}

function GetInnerText (node)
{
    return (node.textContent || node.innerText || node.text) ;
}

 

which is stored as a Javascript file, I then have this HTML

 

<select name="county" id="county" onChange="return ContinentListOnChange()">

<option value="London">London</option>

<option value="New York">New York</option>

<option value="Paris">Paris</option>

</select>

 

So when one of the options is selected (ie: Paris), a new HTML select list is loaded below containing options based on what has been selected.

 

The code for the HTML list which loads is

 

<select name="region" id="region">
<option value="">Select county</option>
</select>

 

This all works fine, but I wonder if anyone can help with my questions.

 

Basically I have a PHP session stored on the page called

 

$_SESSION['county']

 

which can store the name of one of the options, so like

 

$_SESSION['county'] = 'Paris';

 

or

 

$_SESSION['county'] = 'London';

 

So my question is, how can I provide the javascript a default "county" to load data for?

 

Kind of a default option.

 

I wonder if it's something to do with doing an "onload" within the <body> tag

 

Any help would be ace, i've tried to many things and cannot get it to work

 

Thanks

 

Dave

Link to comment
Share on other sites

So my question is, how can I provide the javascript a default "county" to load data for?

 

Kind of a default option.

 

I wonder if it's something to do with doing an "onload" within the <body> tag

 

What exactly do you mean with a default country? A default to show on your page, a default value for you session? Or perhaps something else?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.