tags:

views:

452

answers:

1

UPDATED - since I was not clear in me expressions. Will try again:

I have a form with several inputs that are created dynamically like this:

<form id="tellafriend_form" method="post" action="landingpage.aspx">
  <!-- static example -->
  <input type="text" id="my_example" name="my_example" value="" />

  <!-- dynamic part -->
  <xsl:for-each select="something">
  <xsl:variable name="publicationId" select="@id"/>
    <input type="text" id="{$publicationId}" name="{$publicationId}" value="" />
  </xsl:for-each>
</form>

When submitted how do I get the value from the input using xslt? I can get it from the static input field but not from the dynamic fields since I do not know there names/ids.

I know that all $publicationId will be an integer greater than 2000 but less than 4000. If needed they can easily be prefixed with some text (if numbers alone make a problem).

An XSLT solution would be preferred. Or using jQuery if that could do the trick (saw this, that may be another solution: http://stackoverflow.com/questions/169506/get-form-input-fields-with-jquery).

BR. Anders

A: 

landingpage.aspx won't be able to identify the relevant POST values as you never know in advance the input element name.

This suggests you'd need to augment the name with some additional data which you do know in advance. That is, add in extra information to the name that you can check for later.

A good choice is to append the names in such a way that the receiving script is able to (automatically) interpret them as an array. This is then easier to deal with in the receiving script. One such way, dependent on what the receiving language/framework allows, is:

<form id="tellafriend_form" method="post" action="landingpage.aspx">
  <!-- static example -->
  <input type="text" id="my_example" name="my_example" value="" />

  <!-- dynamic part -->
  <xsl:for-each select="something">
  <xsl:variable name="publicationId" select="@id"/>
    <input type="text" id="{$publicationId}" name="publication[{$publicationId}]" value="" />
  </xsl:for-each>
</form>

Try this and examine the publication value in the POST data. You may find this is an array or hashmap. For this to work you would need to use the correct representation for an array of data within a form as far as the receiving language is concerned.

Another option is to augment the input names with a known identifier and then, within the receiving script, check all POST field names for the relevant identifier. For example:

<form id="tellafriend_form" method="post" action="landingpage.aspx">
  <!-- static example -->
  <input type="text" id="my_example" name="my_example" value="" />

  <!-- dynamic part -->
  <xsl:for-each select="something">
  <xsl:variable name="publicationId" select="@id"/>
    <input type="text" id="{$publicationId}" name="publication{$publicationId}" value="" />
  </xsl:for-each>
</form>


Iterate through all the received POST name:value pairs and check for those whose name begins with 'publication'.

For this to work you must choose a prepend value that does not occur within an actual publication ID. I'm assuming here that a publication ID is numeric, hence any meaningful non-numeric prepend value (such as 'publication') would be appropriate.

Jon Cram
Super! Good explanation. And I can do it this way. I just thought of another solution, that could be used if id where not numeric: I create the dynamic input from a given rule (create input for all books in cat=2). If the landing page knows the cat=2 then it can pull the data after the exact same rule (get form input data from names based on books in cat=2).
Tillebeck