Advertisement

06.06.2005 at 08:43AM PDT, ID: 21448236
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

populate and set selected index for selectbox

Asked by ellandrd in JavaScript

Tags: , , ,

i have a 2 select boxes

1 selectbox for the "country"
1 selectbox for the "county"

when user selects a country, the second selectbox is populated with that countries countys

this all works fine and after the user submits the form, the values are inserted into a DB. no problems so far.

i have another page that pulls the values from the DB into a form again so the user can edit the value.

what im want is to populate the 2 select boxes with the all the countries and countys again and set the selected index to the values got from the DB.

i have set the 1st select box Country/County to Scotland/Aberdeen as default

here is my .js page

var county = new Array("Aberdeen","Aberdeenshire","Angus","Argyll and Bute","Ayrshire","Clackmannanshire","Dumfries and Galloway","Dunbartonshire","Dundee","East Lothian","Edinburgh","Falkirk","Fife","Glasgow","Highland","Inverclyde","Lanarkshire","Midlothian","Moray","Orkney","Perth and Kinross","Renfrewshire","Scottish Borders","Shetland","Stirling","West Lothian","Western Isles")

var country = new Array("Scotland","Republic of Ireland")

ar[0] = new Array();
ar[0][0] = new makeOption("Aberdeen");
ar[0][1] = new makeOption("Aberdeenshire");
ar[1] = new Array();
ar[1][0] = new makeOption("Carlow");
ar[1][1] = new makeOption("Cavan");

function makeOption(text){
      this.text = text;
}

function relate2(form){
        var options = form.county.options;
        for (var i = options.length - 1; i > 0; i--){
          options[i] = null;
        }
        var curAr = ar[form.country.selectedIndex];
        for (var j = 0; j < curAr.length; j++){
          options[j] = new Option(curAr[j].text);
        }
        options[0].selected = true;
}


here is my .php page:

first i read the value form DB then display all values in table...

$development_county = $row['development_county'];
$development_country = $row['development_country'];

....


<tr>
                                    <td style="width:10%;"><p><b>Country</b></p></td>
                                    <td style="width:40%;"><p>
                                    <select name="development_country" style="width:286px;" onChange="relate2(this.form)">
                                          <script type="text/javascript">
                                          <!--
                                          for(i = 0; i < country.length; i++)
                                          {
                                                document.write("<option value='" + country[i] + "'>" + country[i] + "</option>");
                                          }

                                          var selectBox = document.form.development_country;

                                          for (var no = 0; no < selectBox.options.length; no++)
                                          {
                                                if (selectBox.options[no].text == '<?=$development_country?>')
                                                {
                                                      selectBox.selectedIndex = no;
                                                      break;
                                                }
                                          }

                                          //-->
                                    </script>
                                    </select></p>
                                    </td>
                              </tr>
                              <tr>
                                    <td style="width:10%;"><p><b>County</b></p></td>
                                    <td style="width:40%;"><p>
                                    <select name="development_county" style="width:286px;">
                                    <script type="text/javascript">
                                    <!--
                                    for(i = 0; i < county.length; i++)
                                    {
                                          document.write("<option value='" + county[i] + "'>" + county[i] + "</option>");
                                    }

                                    var selectBox = document.form.development_county;

                                    for (var no = 0; no < selectBox.options.length; no++)
                                    {
                                          if (selectBox.options[no].text == '<?=$development_county?>')
                                          {
                                                selectBox.selectedIndex = no;
                                                break;
                                          }
                                    }

                                    //-->
                                    </script>
                                    </select></p>
                                    </td>
                              </tr>


where is the problem is, is in the countys selectbox, this select box dont get popluted.

the countries selectbox gets populated because it can refer to the country array in the .js file but i only have an hardcored array for the scottish county and not the irish ones

what im after is populate the 2 select boxes with all countries and corresponding countys from .js file and then set the selected index of both select boxes there values from the db match...Start Free Trial
 
 
[+][-]06.06.2005 at 08:45AM PDT, ID: 14154299

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: set, javascript, selected, index
Sign Up Now!
Solution Provided By: archrajan
Participating Experts: 1
Solution Grade: B
 
 
[+][-]06.06.2005 at 08:57AM PDT, ID: 14154422

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.06.2005 at 09:08AM PDT, ID: 14154539

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.06.2005 at 09:53AM PDT, ID: 14154998

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.06.2005 at 10:42AM PDT, ID: 14155417

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.06.2005 at 10:52AM PDT, ID: 14155492

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-42