Adding to a List

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

    Adding to a List

    I want to add items to a list and then submit these items via POST in a
    form. Below is my code and it adds the items fine but they are not selected
    when I submit the form how can I have them all selected when I submit the
    form?

    Any help would be most appreciated!!!

    Thanks in advance!!

    <script type="text/javascript">
    function addOption(selec tObject,optionT ext,optionValue ) {
    var optionObject = new Option(optionTe xt,optionValue)
    var optionRank = selectObject.op tions.length
    selectObject.op tions[optionRank]=optionObject
    }

    function deleteOption(se lectObject,opti onRank) {
    if (selectObject.o ptions.length!= 0) {
    selectObject.op tions[optionRank]=null }
    }

    function testAdd() {
    var formObject = document.testFo rm
    if (formObject.opt ionText.value!= "" && formObject.opti onValue.value!= "")
    {
    addOption(formO bject.fruitList ,formObject.opt ionText.value,f ormObject.optio nValue.value)
    } else {
    alert("Fill form and click Add")
    }
    }

    function testDelete() {
    var formObject = document.testFo rm
    if (formObject.fru itList.selected Index!=-1) {
    deleteOption(fo rmObject.fruitL ist,formObject. fruitList.selec tedIndex)
    } else {
    alert("Select an option and click Delete")
    }
    }
    </script>

    <!---- ***** Start of the Form ***** ---->


    <form action="testa.c fm" method="post" name="testForm" >
    <input name="JOHN" type="text" value="123">
    <p>
    <select name="fruitList " size="10" multiple>
    <option value="TEST" selected>TEST</option>
    </select>
    <br/>
    Fill form and click Add :<br/>
    Option Text : <input type="text" name="optionTex t"/>
    Option Value : <input type="text" name="optionVal ue"/>
    <input type="button" value="Add" onclick="testAd d()"/><br/>
    Select an option and click Delete : <input type="button" value="Delete"
    onclick="testDe lete()"/>
    </p>
    <p>
    <input type="submit" name="Submit" value="Submit">
    </p>
    </form>


  • undercups

    #2
    Re: Adding to a List

    I had a similar problem with a listbox that I was adding or removing
    values from. The answer I found was to have a hidden input field and
    when the page is submitted use javascript to append the values in the
    listbox to the hidden input field, each value having a delimiter.
    Using server side code (VB.Net in mycase) pick out the values from the
    hidden field and process as required.

    Hope this helps

    Duncan

    Comment

    • Rick

      #3
      Re: Adding to a List

      I just finally wrote a script that selected all the items in the list and
      then ran a loop on the next page enter them in the database.


      "undercups" <dwang@woodace. co.uk> wrote in message
      news:1116675380 .448978.60170@f 14g2000cwb.goog legroups.com...[color=blue]
      >I had a similar problem with a listbox that I was adding or removing
      > values from. The answer I found was to have a hidden input field and
      > when the page is submitted use javascript to append the values in the
      > listbox to the hidden input field, each value having a delimiter.
      > Using server side code (VB.Net in mycase) pick out the values from the
      > hidden field and process as required.
      >
      > Hope this helps
      >
      > Duncan
      >[/color]


      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Adding to a List

        Rick wrote:
        [color=blue]
        > [...]
        > <!---- ***** Start of the Form ***** ---->[/color]
        ^ ^------------------------------------------------.
        `-------------------------. |
        Invalid. The comment starts here (with `--') and ends here
        (also with `--').
        [color=blue]
        > [...][/color]

        Your markup is neither Valid HTML nor Valid XHTML.

        <http://validator.w3.or g/>


        PointedEars

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Adding to a List

          Rick wrote:
          [color=blue]
          > I just finally wrote a script that selected all the items in the list
          > and then ran a loop on the next page enter them in the database.[/color]

          Unnecessary and inefficient. If you would have used

          <select ... multiple>
          ...
          </select>

          (HTML) or

          <select ... multiple="multi ple">
          ...
          </select>

          (XHTML) you would have been able to do

          var x = new Option(...);
          ...
          x.selected = true;
          if (!x.selected) x.selected = "selected";

          which would have submitted the selected items only.
          [color=blue]
          > [Top post][/color]

          And if you would have read the newsgroup's FAQ
          and FAQ notes, you would not have been scored down.


          PointedEars

          Comment

          Working...