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
https://forums.phpfreaks.com/topic/199653-xmlhttp-help/
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
https://forums.phpfreaks.com/topic/199653-xmlhttp-help/#findComment-1047904
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.