Search code examples
xformsxsltforms

Send XML element as HTTP header using XForms


I am quite new to XForms so please bear over with me. I would like to know if following can be done:

I have an HTML form

<form>
    <input type="text" name="search-string"/>
    <input type="checkbox" name="search1" checked="checked" />Search option 1
    <input type="checkbox" name="search2" checked="checked" />Search option 2
    <input type="submit" />
</form>

I would like to represent this in XForms and on submit convert it to an XML element like this:

<data>
    <search1>my search string</search1>
    <search2>my search string</search2>
</data>

The search1 and search2 elements should only be set if the corresponding checkboxes are set, and they are both populated with the string from the search-string input.

When the data element has been constructed I need to send it through a POST http request.

Can all this be done using solely XForms or do I need to employ JS or something?

PS: I am using XSLTForms if it makes any difference.


Solution

  • Yes, this can be done with XForms without extra Javascript instructions. You need two instances: one to be submitted and another one to be used to fill the first one.

    This should be something like this for the model part:

    <xf:model>
      <xf:instance id="data">
        <data xmlns="">
          <search1/>
          <search2/>
        </data>
      </xf:instance>
      <xf:instance id="work">
        <work xmlns="">
          <b1 xsi:type="boolean"/>
          <b2 xsi:type="boolean"/>
          <search/>
        </work>
      </xf:instance>
      <xf:bind nodeset="instance('data')/search1" calculate="choose(instance('work')/b1,instance('work')/search,'')"/>
      <xf:bind nodeset="instance('data')/search2" calculate="choose(instance('work')/b2,instance('work')/search,'')"/>
      <xf:submission ref="instance('data') method="post" resource=".........."/>
    </xf:model>
    

    -Alain