• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Must populate a select box onClick

I have dynamically loaded select boxes in a for loop. When the page first loads the select boxes should be empty because we dont want them all to load info from the DB at the same time. However, when any of the boxes is clicked on I need it to reload them populated with options.
How would I go about doing this?
Below is the simple code for how I print my select boxes. But Not sure how to reload them. I also included the function that populates the select box with options, but it doesn't work. Please, help.  

Loop2 : for($i =1; $i <= $MaximumColumn; $i++){
   if  ($result eq "") {
      if ($Updatable == 1) {
           print ("<td height='5' width='500'>Value Not Assigned$tdcls<td><select          name='$selectedAttr' dir=\"rtl\" width=\"20\" onclick=\"javascript: PopulateSelectBox($selectedAttr)\"></td></tr>");                                                          }else {
         print "<td height='5' width='600'>Value Not Assigned</td></tr>";

sub PopulateSelectBox(selectedAttr){
     #print("Inside Populate SelecBox");
     $sqlCmd2 = "select distinct $selectedAttr from RiskXpress order by $selectedAttr ";
          @AttributePickList = &sql($dbpr,$sqlCmd2);
          #print("Inside Populate SelecBox2");
          L1: foreach $line (@AttributePickList) {
               $line =~ s/ *~ */~/g;
               ($Value) = split(/~/,$line);
               if (!$Value){next L1;}
                $Value = rm_junk_char_display($Value);
                print("Inside Populate box3");          
                print "<option value=\"$Value\">$Value</option>";
1 Solution
I assume the select box is embedded in an HTML FORM.

The first time the page is loaded, you would have to replace:
onclick=\"javascript: PopulateSelectBox($selectedAttr)\">

by a javascript function call that changes the FORM action to something like PopulateSelectBox.cgi?selectname=SOMENAME, and then submit the form.

Within PopulateSelectBox.cgi, you can invoke your
 sub PopulateSelectBox(selectedAttr){
with the selectedAttr argument equal to (e.g.) SOMENAME, which will print the necessary HTML code to construct your select box. Of course, this time around you should not define the same onclick action for your select box, otherwise when attempting to select an option, the form will be re-submitted etc.

Hope this helps.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now