问题描述:

I have a form where I am creating multiple multiple selects. So each time I give a name to each select. Like

<?php

$dummy = 0

..........

Loop(1,2,3)

.........

Loop(1,2,3)

echo '<td><select name=uperms' . $dummy . 'multiple>';

$dummy ++;

echo '</select></td>';

...

..

?>

Now in actual the Loop will traverse dynamically but I am sure that each time select having multiple options is created with different name.

I am not sure when form is submitted how many selects has been created so that I can read from each select selected options.

Like values set in select with name uperms0 , uperms1 ...upermsN

Below is the script that I have written:

<script type="text/javascript">

function submitform()

{

var Form = document.forms.perm;

var Selval = "";

var x = 0;

for (x=0; x < Form.uperms0.length; x++)

Selval = Selval + " " + Form.uperms0.value;

alert(Selval);

document.forms["perm"].submit();

}

</script>

But it can read for one select options only? As such first I need to know how many selects are there and then loop thru each select name id?

网友答案:

Select elements have a multiple property that returns true if they are a multiple select. To get the values of selected options, you must loop over them and collect the values of those that are selected.

Forms have an elements property that is a collection of all the controls in the form, so given a reference to the form, you can do:

function getValues(form) {
  var controls = form.elements;
  var value;

  for (var i=0, iLen=controls.length; i<iLen; i++) {

    // Controls must have a name to be successful
    if (controls[i].name != '') {
      value = getControlValue(controls[i]);

      // do something with value
      console.log(value)
    }
  }
}

To get the value:

// Return an array of values for the control
function getControlValue(control) {
  var values = [];

  // Get the value of select elements
  if (control.tagName.toLowerCase() == 'select') {
    if (control.multiple) {
      for (var i=0, iLen=control.options.length; i<iLen; i++) {
        if (control.options[i].selected) {
          values.push(control.options[i].value);
        }
      }
    } else {
      values.push(control.value);
    }
  }

 // Get the values of other controls...

  return values;
}

Some play markup:

<form>
  <select name="foo">
   <option value="0">0
   <option value="1">1
   <option value="2">2
   <option value="3">3
  </select>
  <select name="bar" multiple>
   <option value="0">0
   <option value="1">1
   <option value="2">2
   <option value="3">3
 </select>

 <input type="button" onclick="getValues(this.form)" value="get values">
</form>
网友答案:

You could create a hidden field containing the final value of $dummy. e.g.

<input type="hidden" name="noPerms" value="<?php echo $dummy; ?>" />

Then, in your PHP script, you can grab the value from your hidden and create a while loop that will grab the corresponding select values. E.g.

<?php
    $noPerms = $_POST['noPerms'];
    $x = 0;
    while($x <= $noPerms) {
        echo $_POST['uperms'.$x];
        $x++;
    } 
?>
相关阅读:
Top