Modifying form data before submission

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

    Modifying form data before submission

    I have a form which houses basic inputs, as well as a few multiple select
    forms.

    I need to parse all the 'values' which are in this multiple select form (it
    gets manipulated dynamically client side). I wanted to create a function
    that loops through all the form elements, and if the type 'select-multiple'
    is detected, gather the VALUES of each <option> inside it.

    The bit I'm stuck on is then how to reconstruct this into a form submission.
    Preferably I wanted the data from the 'select-multiple' element to be
    submitted as comma delimited, eg 14,12,512,63,62 which would later be parsed
    by PHP.

    I was considering reconstructing a query string and submitting that as GET,
    but I'd prefer to POST it along with the other data (such as INPUTS etc)
    which do not need to be changed.

    Can I somehow attach some 'hidden' type data onto the end of the form
    submission at this point?

    Thanks in advance


  • Chris Wright

    #2
    Re: Modifying form data before submission

    On Fri, 25 Jul 2003 17:43:45 +1000, "Logical" <logic@privacy. net>
    wrote:
    [color=blue]
    >I have a form which houses basic inputs, as well as a few multiple select
    >forms.
    >
    >I need to parse all the 'values' which are in this multiple select form (it
    >gets manipulated dynamically client side). I wanted to create a function
    >that loops through all the form elements, and if the type 'select-multiple'
    >is detected, gather the VALUES of each <option> inside it.
    >
    >The bit I'm stuck on is then how to reconstruct this into a form submission.
    >Preferably I wanted the data from the 'select-multiple' element to be
    >submitted as comma delimited, eg 14,12,512,63,62 which would later be parsed
    >by PHP.
    >
    >I was considering reconstructing a query string and submitting that as GET,
    >but I'd prefer to POST it along with the other data (such as INPUTS etc)
    >which do not need to be changed.
    >
    >Can I somehow attach some 'hidden' type data onto the end of the form
    >submission at this point?
    >[/color]
    Check the Netscape JavaScript site at http://devedge.netscape.com/
    and look at the CFormData JAvaScript object.

    That might do the trick for you.

    Comment

    • DWilliams

      #3
      Re: Modifying form data before submission

      First off, it's not really clear to me if you want the selected values from
      a multiple select element, or just all of the values regardless of selection
      state. In the case of the former (selected), you can just re-name your
      multiple select element using this syntax:

      <select name="manyselec ts[]" size="10" multiple="multi ple">

      That can be parsed/interpreted because it is already construed as an array
      by PHP...

      echo print_r($_POST['manyselects']);

      In the case of the latter, you would have to implement a javascript
      construct to loop through the select elements on the form before submittal
      to collect the values. Perhaps something like the following...(un tested)

      The intention would be to get a comma separated string for all the
      multiple-select option values. You could then perhaps assign that value to
      a hidden form variable:


      function processMultiple s() {
      var thisSelect;
      var multiValues;
      var theForm = window.document .forms[0];

      for (var i = 0; i < theForm.length; i ++) {
      if (theForm.elemen ts[i].type == "select-multiple") {
      thisSelect = theForm.element s[i];
      for (var j = 0; j < thisSelect.leng th; j ++)
      multiValues += thisSelect.opti ons[j].value + ',';
      }
      }

      // strip last comma
      multiValues = multiValues.sub str(0, multiValues.len gth - 1);

      return multiValues;
      }


      --
      HTH,
      DWilliams

      "Logical" <logic@privacy. net> wrote in message
      news:bfqmp5$h77 qg$1@ID-159445.news.uni-berlin.de...[color=blue]
      > I have a form which houses basic inputs, as well as a few multiple select
      > forms.
      >
      > I need to parse all the 'values' which are in this multiple select form[/color]
      (it[color=blue]
      > gets manipulated dynamically client side). I wanted to create a function
      > that loops through all the form elements, and if the type[/color]
      'select-multiple'[color=blue]
      > is detected, gather the VALUES of each <option> inside it.
      >
      > The bit I'm stuck on is then how to reconstruct this into a form[/color]
      submission.[color=blue]
      > Preferably I wanted the data from the 'select-multiple' element to be
      > submitted as comma delimited, eg 14,12,512,63,62 which would later be[/color]
      parsed[color=blue]
      > by PHP.
      >
      > I was considering reconstructing a query string and submitting that as[/color]
      GET,[color=blue]
      > but I'd prefer to POST it along with the other data (such as INPUTS etc)
      > which do not need to be changed.
      >
      > Can I somehow attach some 'hidden' type data onto the end of the form
      > submission at this point?
      >
      > Thanks in advance
      >
      >
      >[/color]


      Comment

      Working...