Populate ListBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Goodman

    #1

    Populate ListBox

    I have a listbox named sub1 on an asp page. I need to fill this list with
    values from a table. These are selected based upon the selection of a
    value/s in another listbox.
    It was suggested that I use a hidden iframe on my main page, & carry out the
    processing inside the frame, using jscript.

    If I open the page in a browser, & make a selection in the first listbox
    (named main), the data in the frame appears to be processed correctly, the
    following jscript is returned to the browser (in the iframe):

    <script language="JavaS cript" type="text/javascript">
    var objElement = window.parent.d ocument.getElem entById("sub1") ;

    objElement[objElement.leng th] = new Option(Jon, 1);

    objElement[objElement.leng th] = new Option(James, 2);

    objElement[objElement.leng th] = new Option(Audit, 13);

    </script>

    To me this looks pretty much correct as no errors are generated (although I
    do not know jscript).

    So my question is, how do I take the next step; how do I get these correctly
    attained values into my listbox?


    --
    Cheers,

    James Goodman MCSE, MCDBA



  • lallous

    #2
    Re: Populate ListBox

    Hello,

    In order to fill a listbox from say ASP or PHP, you can do such (PHP
    example):

    <html>
    <body>
    ...
    ..
    ..
    ..
    <form>
    <select name='listbox1' >
    <?
    $result = mysql_query("SE LECT id, name FROM users");
    while ($r = mysql_fetch_arr ay($result))
    {
    if ($somecondition )
    echo sprintf("<optio n value='%s'>%s</option>\n", $r['id'], $r['name']);
    }
    ?>
    </select>
    </form>
    ..
    ..
    </body></html>

    Your solution is a bit heavy, to emit such code:[color=blue]
    > objElement[objElement.leng th] = new Option(James, 2);
    >
    > objElement[objElement.leng th] = new Option(Audit, 13);[/color]

    Better that you create a 2D array or two parallel arrays as:
    var x=['James', 'Audit'];
    var y=[2, 13];

    Then using a for loop you can dynamically populate the list box from the JS
    arrays.

    HTH,
    Elias
    "James Goodman" <j a m e s@norton-associates.co.u k> wrote in message
    news:bv8lqg$rbu $1@sparta.btint ernet.com...[color=blue]
    > I have a listbox named sub1 on an asp page. I need to fill this list with
    > values from a table. These are selected based upon the selection of a
    > value/s in another listbox.
    > It was suggested that I use a hidden iframe on my main page, & carry out[/color]
    the[color=blue]
    > processing inside the frame, using jscript.
    >
    > If I open the page in a browser, & make a selection in the first listbox
    > (named main), the data in the frame appears to be processed correctly, the
    > following jscript is returned to the browser (in the iframe):
    >
    > <script language="JavaS cript" type="text/javascript">
    > var objElement = window.parent.d ocument.getElem entById("sub1") ;
    >
    > objElement[objElement.leng th] = new Option(Jon, 1);
    >
    > objElement[objElement.leng th] = new Option(James, 2);
    >
    > objElement[objElement.leng th] = new Option(Audit, 13);
    >
    > </script>
    >
    > To me this looks pretty much correct as no errors are generated (although[/color]
    I[color=blue]
    > do not know jscript).
    >
    > So my question is, how do I take the next step; how do I get these[/color]
    correctly[color=blue]
    > attained values into my listbox?
    >
    >
    > --
    > Cheers,
    >
    > James Goodman MCSE, MCDBA
    > http://www.angelfire.com/sports/f1pictures
    >
    >
    >[/color]


    Comment

    • @SM

      #3
      Re: Populate ListBox

      James Goodman a ecrit :[color=blue]
      >
      > I have a listbox named sub1 on an asp page. I need to fill this list with
      > values from a table. These are selected based upon the selection of a
      > value/s in another listbox.
      > It was suggested that I use a hidden iframe on my main page, & carry out the
      > processing inside the frame, using jscript.
      >
      > If I open the page in a browser, & make a selection in the first listbox
      > (named main), the data in the frame appears to be processed correctly, the
      > following jscript is returned to the browser (in the iframe):
      >
      > <script language="JavaS cript" type="text/javascript">
      > var objElement = window.parent.d ocument.getElem entById("sub1") ;[/color]
      [color=blue]
      > objElement[objElement.leng th] = new Option(Jon, 1);
      >
      > objElement[objElement.leng th] = new Option(James, 2);
      >
      > objElement[objElement.leng th] = new Option(Audit, 13);
      >
      > </script>
      >
      > To me this looks pretty much correct as no errors are generated (although I
      > do not know jscript).
      >
      > So my question is, how do I take the next step; how do I get these correctly
      > attained values into my listbox?[/color]

      ? ?
      I think doing that you added to your listbox "sub1"
      the options :
      <option value="Jon">1
      <option value="James">2
      <option value="Audit">3

      ==== To run it ========

      <html>
      <script type="text/javascript">
      function add(list,val,tx t) {
      if(document.get ElementById) {
      var objElement = window.parent.d ocument.getElem entById(list);
      objElement[objElement.leng th] = new Option(txt,val) ; }
      else { // if DOM is not understood by the browser
      var objElement = parent.document .forms[0].elements[list].options;
      // new index for options = length of array of options
      // and immediatly increase this length ( = create a new option)
      objElementNewIn dex = objElement.leng th++;
      with(objElement[objElementNewIn dex]) { value=val; text=txt; }
      }
      }
      sp='http://perso.wanadoo.f r/stephane.moriau x/merci.htm';
      </script>
      <form><select id="sub1" name="sub1" onchange="o=thi s.options[this.options.se lectedIndex];
      if(o.text=='Ste phane') {location.href= o.value;}
      else alert(o.value); ">
      <option>See here added options
      </select></form>
      <p><a href="#" onclick="add('s ub1','Jon',1);" >Add Jon</a>
      <p><a href="#" onclick="add('s ub1',sp,'Stepha ne');">Add @SM</a>
      <p><a href="#" onclick="add('s ub1','Jack',3); ">Add Jack</a>
      </html>

      ====== Other example =============== ===>
      (to get a list feed by an other list )

      <html>
      <script type="text/javascript">
      function ajouter(){ // JS of our grd' fathers running every where
      var o = document.forms['formOne'].elements['liste'].options;
      var o = o[o.selectedIndex];
      var w = document.forms['formTwo'].elements['liste'].options;
      var l = w.length++;
      with(w[l]) { text=o.text; value=o.value; }
      }

      // only to see something
      function valid(){ // copy of last choice in text fields
      var d = document.forms['formulaire'];
      var o = d.elements['liste'].options;
      o = o[o.selectedIndex];
      d.elements['saisie'].value=o.text;
      d.elements['aller'].value=o.value;
      }
      </script>
      <form name=formTwo>
      <p><select name=liste >
      <option value=0>See new list
      </select>
      </form>
      <form name=formOne>
      <p><select name=liste >
      <option value=0>Choice a file
      <option value="009.htm" >009
      <option value="010.htm" >010
      <option value="011.htm" >011
      <option value="012.htm" >012
      </select>
      <p>then click one of these buttons :
      <br><input type=button value="Test with info"
      onclick="valid( );ajouter();">
      <input type=button value="Direct Test"
      onclick="ajoute r();">
      <p>The New Option :
      <br>Its text : <input type=text name=saisie>
      <br>Its value : <input type=text name=aller>
      </form></html>

      --
      ******** (enlever/remove [OTER_MOI] du/from reply url) *******
      Stéphane MORIAUX : mailto:stephane OTER_MOImoriaux @wanadoo.fr
      Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)

      *************** *************** *************** *************** **

      Comment

      Working...