Welcome Guest, Not a member yet? Register   Sign In
JavaScript INVERT Select form
#1

[eluser]Mitus[/eluser]
Hi, I have a script that I want to submit but the unselected values. But there is a Bug. How can that be corrected then? Here is the code:
Code:
function loopSelected(frm)
{
  var txtSelectedValuesObj = document.getElementById('txtSelectedValues');
  var selectedArray = new Array();
  var selObj = document.getElementById('selSeaShells');
  var i;
  var count = 0;
  for (i=0; i<selObj.options.length; i++) {
    if (selObj.options[i].selected==false) {
      selectedArray[count] = selObj.options[i].value;
      count++;
    }
  }
  txtSelectedValuesObj.value = selectedArray;
  frm.submit(txtSelectedValuesObj.value);
}
This has to run like a normal submit of a form but sending the unselected values.
Code:
&lt;form action="tutorial004_nw.html" method="get"&gt;
  <table border="1" cellpadding="10" cellspacing="0">
  <tr>
    <td valign="top">
      &lt;input type="button" value="Submit"&gt;
      &lt;input type="button" value="Loop Selected"&gt;
      <br />
      <select name="selSea" id="selSeaShells" size="15" multiple="multiple">
        <option value="val0" selected>sea zero</option>
        <option value="val1">sea one</option>
        <option value="val2">sea two</option>
        <option value="val3">sea three</option>
        <option value="val4">sea four</option>
        <option value="val5">adsea one</option>
        <option value="val6">dasea two</option>
        <option value="val7">dasea three</option>
        <option value="val8">dasea four</option>
      </select>
    </td>
    <td valign="top">
      &lt;input type="text" id="txtSelectedValues" /&gt;
      selected array
    </td>
  </tr>
  </table>
&lt;/form&gt;
Has anyone got an Idea on that?
Mitus
#2

[eluser]Nick Husher[/eluser]
Form elements won't be submitted if they lack a name property. Adding a name to the input with the id of txtSelectedValues should do the trick. Remember that in strict XHTML, name properties and id properties share the same namespace.
#3

[eluser]Mitus[/eluser]
Thanks for the Tip. I do not know how that can then be inserted to the code since it already has a name in the form. Thanks again but I am not a JS freak to figure that out. HELP!
#4

[eluser]Pascal Kriete[/eluser]
It doesn't have a name in the code you posted.
Code:
&lt;input type="text" id="txtSelectedValues" /&gt;
#5

[eluser]Mitus[/eluser]
Not still working. So where is the error? The Values not Selected have to be sent.
Code:
&lt;html lang="en" xml:lang="en" &gt;
  &lt;head&gt;
    &lt;META http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;title&gt;gr7xsl_db.xsl&lt;/title&gt;
    [removed]
      &lt;!--
      function delSelected(frm)
      {
        var wertObj = document.getElementById('submit.Delete');
        var selectedArray = new Array();
        var selObj = document.getElementById('wertValues');
        var i;
        var count = 0;
        for (i=0; i<selObj.options.length; i++) {
          if (selObj.options[i].selected==false) {
            selectedArray[count] = selObj.options[i].value;
            count++;
          }
        }
        wertObj.value = selectedArray;
        frm.submit(wertObj.value);
      }
      // --&gt;
  
    [removed]
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form name="index" id="index" action="Index" method="get" accept-charset="UTF-8"&gt;
      &lt;input type="hidden" name="current.Model" value=""&gt;&lt;input type="hidden" name="current.Table" value="">&lt;input type="hidden" name="XSL" value="gr7xsl_db.xsl"&gt;&lt;input type="hidden" name="RowsPerPage" value="999999">
      <div class="contentbox">
        <table class="inhalt">
          <tbody>
            <tr>
              <td valign="top">ZNr<br>Abw.-Kennung / Variante<br>Kommentar<br>Start-/Endbetriebsstelle<br>
                
              </td><td>
                <table>
                  <tr>
                    <td><td>101042<br>22<br>
                        
                        <br>XSB / RB<br>
                      <select id="wertValues" size="30" multiple name="T_GUELTIGKEITEN_GR7_ST.A_ID"><option value="4494">05.04.2009</option><option value="4493">06.04.2009</option<option value="4389">19.07.2009</option> <option value="7246">25.10.2009</option></select></td></td>
                    
                  </tr>
                </table>
              </td>
              <tr>
                <td></td><td>
                  &lt;input type="hidden" name="submit.Update" id="submit.Update" disabled&gt;
                    <button type="reset">Zur&uuml;cksetzen
                  </button>
                  &lt;input type="hidden" name="submit.Update" id="submit.Delete" disabled&gt;
                    <button type="submit" name="frm">Submit
                </button></td>
              </tr>
            </tr>
          </tbody>
        </table>
      </div>
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;
Take a look. Thanks
#6

[eluser]obiron2[/eluser]
the $_POST array will only contain selected items so you have three choices.

1) you should know (or be able to calculate) the values shown in the selection list, so by definition, you know that the ones not returned in the $_POST were not selected.

2) add hidden fields to the form, one for each element in the selection list and use a javascript event onClick() to change the value of the hidden element.

3) a combination of the two. Create hidden elements for each of the selections available and that will be returned as part of the $_POST array. Intersect that with the selected values and you will be left with the unselected values.

the first is probably the easiest to code as a one off, but the last one would provide a generic pattern that could be used across multiple forms; The array you use to build the selection list is also used to build the hidden form elements.

obiron
#7

[eluser]Nick Husher[/eluser]
The specific issue in your code above is that the javascript is never being executed. You should be listening for a submit event on the parent form that will call your javascript function.

Note that obiron's suggestion is a better one: you know what's being displayed, and you know what's being returned. On the server side you can subtract what's being returned from what's being displayed.




Theme © iAndrew 2016 - Forum software by © MyBB