<p>Per Michael's suggestion above, with some help from <a href="https://craftcms.stackexchange.com/questions/4414/search-contact-person-plugin-via-ajax">this answer</a>, I passed the results from the API call back to a craft template as query params, so it looked like this (simplified): </p>

<pre><code>{% if craft.request.isAjax %}
    {% set myReps = craft.request.getParam('personId') %}
    {% set myIds = myReps | split(',') %}
    {% for myId in myIds %}
        {% set person = craft.entries.section('representatives').repId(myId).first() %}
    {% endfor %}
{% endif %}
</code></pre>

<p>and then output my info from there.</p>

CC BY-SA 3.0
2017-08-31T19:44:46.977Z
2017-08-31T19:44:46.977Z
mierla
<p>I've got a frontend form which queries the openstates.org API to return a given set of data objects. </p>

<p>Those objects also exist in Craft as entries, and the API data includes a field that serves as a unique ID for the Craft entries - how can I pass those unique IDs back to the Craft template to retrieve the corresponding entries?</p>

CC BY-SA 3.0
2017-07-26T05:57:29.230Z
You could add them as query string to the URL. When rendering the page, you can tan check the query strign to see which ones you'd have to display...
mrsmn
CC BY-SA 3.0
2017-07-26T15:23:49.347Z
Thanks, Michael - that worked perfectly. I wound up combining it with the [sample code from this answer](https://craftcms.stackexchange.com/questions/4414/search-contact-person-plugin-via-ajax).
mierla
CC BY-SA 3.0
2017-08-01T13:24:25.277Z
Perhaps add your working combination as answer? That way people in the future can directly see the answer.
mrsmn
<templating><javascript>
How can I pass a JS result into a Twig template?
# How can I pass a JS result into a Twig template?

(Asked by: username_0 on 2017-07-25)

I've got a frontend form which queries the openstates.org API to return a given set of data objects.

Those objects also exist in Craft as entries, and the API data includes a field that serves as a unique ID for the Craft entries - how can I pass those unique IDs back to the Craft template to retrieve the corresponding entries?

**Tags:** templating, javascript

> **Comments:**
> **username_1**: You could add them as query string to the URL. When rendering the page, you can tan check the query strign to see which ones you'd have to display...  
> **username_0**: Thanks, Michael - that worked perfectly. I wound up combining it with the [sample code from this answer](https://craftcms.stackexchange.com/questions/4414/search-contact-person-plugin-via-ajax).  
> **username_1**: Perhaps add your working combination as answer? That way people in the future can directly see the answer.  

---

## 1 answers

### Answer by username_0 on 2017-08-31. Score: 2 | Accepted answer

Per Michael's suggestion above, with some help from this answer, I passed the results from the API call back to a craft template as query params, so it looked like this (simplified): 
{% if craft.request.isAjax %}
    {% set myReps = craft.request.getParam('personId') %}
    {% set myIds = myReps | split(',') %}
    {% for myId in myIds %}
        {% set person = craft.entries.section('representatives').repId(myId).first() %}
    {% endfor %}
{% endif %}

and then output my info from there.


---

