SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding a second copy of a script (with different fieldnames) breaks the first one...

    I am a javascript noob, so try to be gentle

    I have a script that is working VERY nicely. It is a chain select script that I got here a few days ago. By itself it is working great with the parent combobox with id name 'zone' and the child with id nam 'subzone'.

    The script follows:
    PHP Code:
    window.onload = function(){
      if(!
    document.getElementsByTagName || !document.createTextNode) return;
      
    initCs();
    }
    var 
    request false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e2) {
    request = false;
    }
    }
    @end @*/
    if (!request && typeof XMLHttpRequest != 'undefined') {
    request = new XMLHttpRequest();
    }

    function 
    fillSelect(zone) {
    var 
    url "csa_ajax.php?zone=" escape(zone);
    request.open("GET"urltrue);
    request.onreadystatechange go;
    request.send(null);
    }

    function 
    go() {
     if (
    request.readyState == 4) {
      if (
    request.status == 200) {
       var 
    response request.responseText;
       var list=
    document.getElementById("subzone");
       var 
    cities=response.split('|');
       for (
    i=1i<cities.lengthi++) {
          var 
    x=document.createElement('option');
          var 
    y=document.createTextNode(cities[i]);
          
    x.appendChild(y);
          list.
    appendChild(x);
       }
       if (
    typeof(startingsubzone) != 'undefined' && startingsubzone != 'alreadyset') {selectsubzone();}
      }
     }
    }

    function 
    initCs() {
    var 
    zone=document.getElementById('zone');
    zone.onchange=function() {
     if(
    this.value!="") {
      var list=
    document.getElementById("subzone");
      while (list.
    childNodes[0]) {
    list.
    removeChild(list.childNodes[0])
    }
      
    fillSelect(this.value);
      }
     }
     
    fillSelect(zone.value);
    }


    // my addition to this code to set the value of the 'subzone' id
    // the startingsubzone variable is a variable passed from post to javascript by some php
    // at the top of the form.
    function selectsubzone(){
    var 
    len document.getElementById("subzone").length -1;
    for(
    i=leni>=0i--){
      if (
    document.getElementById("subzone").options[i].value==startingsubzone){
        
    document.getElementById("subzone").options[i].selected=true;
      }
    }
    startingsubzone='alreadyset';

    This works wonderfully.

    I want to have it do the same thing with 2 sets of listboxes though. Exact same contents, same behavior.

    The second set of listboxes is named zone2 and subzone2. I thought it would be simplest to just make a copy of the chained_select.js, name it chained_select2.js and change all field references to point to the new fields.

    Including both of these files in the form broke the first set of comboboxes. Is this a case of variables interfering? Is it the two onload window events? Are the requests from the first listbox for data being intercepted by the second?

    I have decided I can make this work with just one set of comboboxes, but I would really like to understand what is wrong with all of this. My problem I guess is a fundamental lack of understanding of the variable visibility and lifespan in javascript.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,053
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    names?

    Did you name the XMLHttpRequest objects differently too? ie. request1 and request2

  3. #3
    SitePoint Evangelist Alluvian's Avatar
    Join Date
    Jan 2007
    Location
    Orlando, FL
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At one point I had every variable with a 2 tacked on and even had a second, identical php file.

    In the end I think it was the two competing onload functions.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,053
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)

    onload

    Yes, instead of having more than 1 onload, 1 onload should call multiple functions.


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
  •