Determine contents of listbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tks423@gmail.com

    Determine contents of listbox

    How can I determine the values of a listbox, both unselected or
    selected after submitting the form:

    Code:

    <select name="sel1" size="10" multiple="multi ple">
    <? PHP code populates listbox ?>
    </select>


    Do I have to use Javascript to determine the contents, like in the
    pseudo code Javscript below. But how would you pass the Javascript
    return value to PHP?

    Code:

    function listBoxContents (sel1)
    {
    var selLength1 = sel1.length;
    var valuesPresent1 = new Array(sel1.leng th-1);
    var i;
    for(i=0; i<=selLength1-1; i++)
    {
    valuesPresent1[i]=sel1.options[i].valueOf();
    }

    return valuesPresent1;
    }

  • shimmyshack

    #2
    Re: Determine contents of listbox

    On May 3, 5:45 am, tks...@gmail.co m wrote:
    How can I determine the values of a listbox, both unselected or
    selected after submitting the form:
    >
    Code:
    >
    <select name="sel1" size="10" multiple="multi ple">
    <? PHP code populates listbox ?>
    </select>
    >
    Do I have to use Javascript to determine the contents, like in the
    pseudo code Javscript below. But how would you pass the Javascript
    return value to PHP?
    >
    Code:
    >
    function listBoxContents (sel1)
    {
    var selLength1 = sel1.length;
    var valuesPresent1 = new Array(sel1.leng th-1);
    var i;
    for(i=0; i<=selLength1-1; i++)
    {
    valuesPresent1[i]=sel1.options[i].valueOf();
    }
    >
    return valuesPresent1;
    >
    }
    if you form uses POST just use
    <?php
    print_r($_POST['sel1'])
    ?>
    to see the array you get when you select certain options in the form
    assuming yur markup is correct you will get values. your php code for
    the options in the drop down might have the values for each option
    like so, assuming a loop of some kind:
    $dropdown = '';
    $dropdown .= '<option value="' . $value[$i] .'">'.$value[$i].'</
    option>'."\n";

    Comment

    • Bocah Sableng

      #3
      Re: Determine contents of listbox

      On May 3, 11:45 am, tks...@gmail.co m wrote:
      How can I determine the values of a listbox, both unselected or
      selected after submitting the form:
      >
      Code:
      >
      <select name="sel1" size="10" multiple="multi ple">
      <? PHP code populates listbox ?>
      </select>
      >
      Add hidden elements to your form, set the values same as value of your
      options.
      Untested code:

      <select name="sel1[]" size="10" multiple="multi ple">
      <?php foreach ($arr as $k=>$v) { ?>
      <option value="<?php echo $k; ?>"><?php echo $v; ?></option>
      <?php } ?>
      </select>

      <?php foreach ($arr as $k=>$v) { ?>
      <input type=hidden name="sel1all[]" value="<?php echo $k; ?>">
      <?php } ?>

      HTH.

      Comment

      Working...