ODK Survey: Refer to form variable in Ajax query

Hi all,

Let's say that I have a prompt where I ask the user to select which country they are from and store that value in "origin" variable.

I would like to have an Ajax query that returns all the states from the selected country, like so:

"https://query.yahooapis.com/v1/public/yql?format=json&q=" + encodeURIComponent("select * from geo.states where place='MY_USER_COUNTRY_HERE'")

I have tried to do something like (....)("select * from geo.states where place={{data.origin}}" and place=data('origin') and some variations of that but I get an error every time.

I also tried to add a separate term at the end, like + encodeURIComponent(data('origin')) and a few variations on this but again didn't work.

The errors comes on fetching the data during the query, not on conversion.

Lastly I thought about fetching all states from geo.states, but apparently Yahoo queries won't do that. It will accept select * from geo.countries for example, for but states it needs states of a specific country.

My only idea right now as to how to do that would be to create my own table (community table) with a list of all states (in the world), and then use choice_filter on choice_item like like what is done with a CSV query for cascading select.

Another option would be to find a CSV that has a list of all states of all countries and filter on that using CSV queries instead.

But as a general idea I feel being able to make Ajax queries with terms dependant on form data would be very interesting.

Any thoughts?

Oh just to specify: Createing my own table means creating a table on Yahoo that would contain that information and then call the query on that homemade table. I haven't tried it yet but apparently you can make your own table and call queries on them.

Aaaaaaaah I was missing the quotations on this. I thought it unlikely that {{data.variable}} would work but because the usual JS substitution didn't work I decided to give it a try.

That's the kind of things I would for sure add to the docs.

I'll be a bit busy for the next little while, but since I've been coding for a while on Survey by now I'd be down to add that kind of info to the documentations, if you guys want the help. Would likely be at some point in december I think.

Unfortunately, there is a different syntax for value substitution in
javascript formulas / expressions vs. in the display text (handlebars
templates).

  • *{{data.fieldname}} *is used within *handlebars *templates (e.g.,
    display.text ) to substitute the value of 'fieldname' into the text display.

  • *data('fieldname') *is used within *javascript *expressions to access
    the value of 'fieldname'.

So, using the Javascript variant, you should be able to do something like:

"https://query.yahooapis.com/v1/public/yql?format=json&q=" +
encodeURIComponent("select * from geo.states where place='" +
data('origin')+"'")

Note the single quotes near the double quotes in the above expression.

And, of course, you should cleanse the value of data('origin') so that any
single quotes are properly escaped (to prevent SQL injection attacks).

ยทยทยท On Sun, Sep 14, 2014 at 4:17 AM, wrote:

Oh just to specify: Createing my own table means creating a table on Yahoo
that would contain that information and then call the query on that
homemade table. I haven't tried it yet but apparently you can make your own
table and call queries on them.

--

Post: opendatakit@googlegroups.com
Unsubscribe: opendatakit+unsubscribe@googlegroups.com
Options: http://groups.google.com/group/opendatakit?hl=en


You received this message because you are subscribed to the Google Groups
"ODK Community" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to opendatakit+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com