SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Vancouver, Canada
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only part of the codes is working and I can't find the errors

    I want my online form to automatically select some answers based on values found in the URL.

    For example https://www.website.com/index.php?province=ontario

    I want the province of ontario to be preselected on my form.

    I have 3 variables I want preselected:

    credit
    province
    loantype

    Currently, only the "province" value works. I don't know why the others aren't working

    The javascript code is:

    Code:
          function SelectProvince(province)
          {
          var lb = document.tafform.propertyLocation;
          for( var i=0; i<lb.options.length; i++ )
                 {
             if(lb.options[i].text.toLowerCase() == province.toLowerCase())
                    {
                     var o = new Option();
                     o.text = lb.options[i].text;
                     o.value = lb.options[i].value;
                     o.selected = true;
                     lb.options[i] = o;
                    }
             }
         }
          function SelectLoantype(loantype)
          {
          var lb = document.tafform.typeOfLoan;
          for( var i=0; i<lb.options.length; i++ )
                 {
             if(lb.options[i].text.toLowerCase() == loantype.toLowerCase())
                    {
                     var o = new Option();
                     o.text = lb.options[i].text;
                     o.value = lb.options[i].value;
                     o.selected = true;
                     lb.options[i] = o;
                    }
             }
         }
     
        function SelectCredit(credit)
          {
          var lb = document.tafform.CreditProfile;
          for( var i=0; i<lb.options.length; i++ )
                 {
             if(lb.options[i].text.toLowerCase() == credit.toLowerCase())
                    {
                     var o = new Option();
                     o.text = lb.options[i].text;
                     o.value = lb.options[i].value;
                     o.selected = true;
                     lb.options[i] = o;
                    }
             }
         }
        function SetupPage ()
        {
          var province = this.getQueryValue("province");
          this.SelectProvince(unescape(province));
     
          var loantype = this.getQueryValue("loantype");
          this.SelectLoantype(unescape(loantype));
     
          var credit = this.getQueryValue("credit");
          this.SelectCredit(unescape(credit));
        }
     
        function getQueryValue(name) 
        {
            var url = document.URL; 
            var i = url.indexOf(name += '=');
            var j = url.indexOf('&', i);
            if(-1 == j) {j = url.length;}
            if(-1 != i) {return url.substring(i + name.length, j)};
        }
    An actual URL is:

    https://www.mortgageshoppercanada.co...dtest_GCAMSCBC

    Does anyone see the mistakes?

    Thanks

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try replacing

    Code:
    function SelectProvince(province)
          {
             var lb = document.tafform.propertyLocation;
             for( var i=0; i<lb.options.length; i++ )
                 {
                if(lb.options[i].text.toLowerCase() == province.toLowerCase())
                    {
                        var o = new Option();
                        o.text = lb.options[i].text;
                        o.value = lb.options[i].value;
                        o.selected = true;
                        lb.options[i] = o;
                    }
             }
         }
    
          function SelectLoantype(loantype)
          {
             var lb = document.tafform.typeOfLoan;
             for( var i=0; i<lb.options.length; i++ )
                 {
                if(lb.options[i].text.toLowerCase() == loantype.toLowerCase())
                    {
                        var o = new Option();
                        o.text = lb.options[i].text;
                        o.value = lb.options[i].value;
                        o.selected = true;
                        lb.options[i] = o;
                    }
             }
         }
         
           function SelectCredit(credit)
          {
             var lb = document.tafform.CreditProfile;
             for( var i=0; i<lb.options.length; i++ )
                 {
                if(lb.options[i].text.toLowerCase() == credit.toLowerCase())
                    {
                        var o = new Option();
                        o.text = lb.options[i].text;
                        o.value = lb.options[i].value;
                        o.selected = true;
                        lb.options[i] = o;
                    }
             }
         }
    
        function SetupPage ()
        {
          var province = this.getQueryValue("province");
          this.SelectProvince(unescape(province));
          
          var loantype = this.getQueryValue("loantype");
          this.SelectLoantype(unescape(loantype));
          
          var credit = this.getQueryValue("credit");
          this.SelectCredit(unescape(credit));
        }
    with

    Code:
                function selectValue(el, value) {
                    for(var i = 0; i < el.options.length; i++) {
                        if(el.options[i].text.toLowerCase() == value) {
                            el.selectedIndex = i;
                            break;
                        }
                    }
                }
                function SetupPage () {
                    var province = this.getQueryValue("province");
                    selectValue(document.getElementById('propertyLocation'), province);
                    var loantype = this.getQueryValue("loantype");
                    selectValue(document.getElementById('typeOfLoan'), loantype);
                    var credit = this.getQueryValue("credit");
                    selectValue(document.getElementById('CreditProfile'), credit);
                }
    edit: Add an ID attribute to the drop down boxes, and name it the same as its name attribute.


  3. #3
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Vancouver, Canada
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried the codes and added "ID" to the selection box with the same value as the "name". IT's not working. Now even the "province" won't work.

    Not sure when I went wrong.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry the value being passed wasn't being checked with toLowerCase()

    Code:
                function selectValue(el, value) {
                    value = value.replace('&#37;20',' ');
                    for(var i = 0; i < el.options.length; i++) {
                        if(el.options[i].text.toLowerCase() == value.toLowerCase()) {
                            alert(el.options[i].text);
                            el.selectedIndex = i;
                            break;
                        }
                    }
                }


  5. #5
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Vancouver, Canada
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's working now.

    Thanks so much gRoberts.


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
  •