1. Home
  2. Data Collection
  3. How to: Pre-fill form fields using Enketo Express

How to: Pre-fill form fields using Enketo Express

If there are multiple fields you would like to prefill, then you would have to separate each of the other query parameters with an ampersand like the following example:

https://enketo.ona.io/_/?d[/xpath/to/field_name]=<prefill-value>&d[/xpath/to/other_field_name]=<prefill-value>#ABC2

Note:
The characters after <prefill-value> will change for each form and are dependent on the URL assigned to the Enketo webform. For example, for the form with a URL https://enketo.ona.io/x/#ABC2, you would append #ABC2 after <prefill-value> as shown in the example above.

To retrieve the XPath of the field, you would need the xml of the form. You can retrieve the links to the xml representation of the forms you have access to by visiting https://odk.ona.io/<your-username>/formListand copying the contents in between downloadUrl tags for the specific form i.e when you visit https://odk.ona.io/<your-username>/formList, you will get something similar to the following:

 <xforms xmlns="http://openrosa.org/xforms/xformsList">
     <xform>
         <formID>form_a</formID>
         <name>Form A</name>
         <majorMinorVersion/>
         <version/>
         <hash>md5:64bdafcee354f536c99a790ab1668330</hash>
         <descriptionText/>
         <downloadUrl>https://odk.ona.io/john/forms/1234/form.xml</downloadUrl>
         <manifestUrl>https://odk.ona.io/john/xformsManifest/1234</manifestUrl>
     </xform>
     <xform>
         <formID>form_B</formID>
         <name>Form B</name>
         <majorMinorVersion/>
         <version/>
         <hash>md5:83436c39306a2e982ccc7ce28e12fe0b</hash>
         <descriptionText/>
         <downloadUrl>https://odk.ona.io/john/forms/2345/form.xml</downloadUrl>
         <manifestUrl>https://odk.ona.io/john/xformsManifest/2345</manifestUrl>
     </xform>
 </xforms>

From the above example, to get the xml representation of Form B, follow the steps below:

  • Copy text in between the tag downloadUrlin our case https://odk.ona.io/jane/forms/2345/form.xmland open in a new tab;
  • Once you get to the webpage, right-click and select View Page Source to open the xml page;
  • The section that has the XPath to the fields in that particular form has bind tags and the XPath value can be found on the nodeset attribute on the same tag.

See the example below:

...
<bind nodeset="/form_B/browser_name" type="select"/>
<bind nodeset="/form_B/age_limit" type="int"/>
<bind nodeset="/form_B/current_location" type="geopoint"/>

This means that if I wanted to prefill the field age_limit when the webform loads, my url would be as follows:

https://enketo.ona.io/_/?d[/form_B/age_limit]=45#ABC2

Note:
The xpath is inside square brackets and it’s preceeded with letter d.

The format for multiple choice questions would be the same, however, the value has to be one of the names you have set on the choices worksheet for that specific field. Additionally, if you have multiple select fields and you would like to prefill more than one option, you will have to separate your value with %20 which means space. See the example below:

https://enketo.ona.io/_/?d[/form_B/multiple_select_field]=value1%20tvalue2

The url above means that the field multiple_select_field, which is a multiple-select field, will have options with names value1 and value2 selected when the form loads.

If you have a geopoint field, the format would be:

https://enketo.ona.io/_/?d[/test/locate]=<latitude>%20<longitude>#ABC2

This means you might have something like:

https://enketo.ona.io/_/?d[/test/locate]=-2.3%2033.788<longitude>#ABC2

Related Articles

Need Support?
Can't find the answer you're looking for?
Contact Support