We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Trouble adding entry_id as JS variable in JS for loop

How Do I?

MattStrybis's avatar
MattStrybis
4 posts
9 years ago
MattStrybis's avatar MattStrybis

Hey there guys,

First time posting so please forgive any bad conduct. 😛

I am trying to display search results from an AJAX request on a new page by looping over expressionengine tags and injecting the entry_id param as a JS variable.

This seems to work when hardcoding the entry_id, but as soon as adding the variable there is no result.

Please view code below. Any help would really go a long way at this point!

Many thanks!

$.getJSON( "/searchResults.json", function( properties ) {
 for (i = 0; i < properties.length; i++) {
  var id = properties[i].propertyId;
         var html = "{exp:channel:entries channel='properties' search:property_id='" + id + "'}           {title}{/exp:channel:entries}";
$(".titles").append(html);
        };
});
       
FountainInternet's avatar
FountainInternet
53 posts
9 years ago
FountainInternet's avatar FountainInternet

That’s not going to work because the channel entries tag executes on the server before the javascript is executed on the client.

       
MattStrybis's avatar
MattStrybis
4 posts
9 years ago
MattStrybis's avatar MattStrybis

I thought that the case, but hard coding in an entry_id wouldn’t work either? Correct me if I am being a noob..

       
FountainInternet's avatar
FountainInternet
53 posts
9 years ago
FountainInternet's avatar FountainInternet

No, because of this bit:

search:property_id='" + id + "'

Right there you’re mixing server-side code (search:property_id) and JS code (id).

       
MattStrybis's avatar
MattStrybis
4 posts
9 years ago
MattStrybis's avatar MattStrybis

Oh right! I see where you’re coming from. Thanks so much.

Do you have any ideas on what the best method to parse entries out that contain a key received from an API json array?

I am attempting to build a search bar that takes a number of parameters (date, location, price, etc) needed for the call and then returns results in a new page.

Going around in circles here.

Right now, I have a form that saves the form vaules in a session storage variable, navigates to a new page, assembles the variables into a url and then runs an ajax request for the json.

The json then returns the key of entries that meet the requirements.

Any help would be much much appreciated.

       
FountainInternet's avatar
FountainInternet
53 posts
9 years ago
FountainInternet's avatar FountainInternet

Your best bet is probably to do create an AJAX endpoint where the backend code delivers a result set ready for use in the frontend without needing to go back to the server again. Deliver the results from the endpoint in JSON format and then loop through the results in JS. A bit like what you’ve done now, but you’ll need to do additional processing in the backend code so the result set can be looped through in the JS without additional AJAX calls.

       
MattStrybis's avatar
MattStrybis
4 posts
9 years ago
MattStrybis's avatar MattStrybis
Your best bet is probably to do create an AJAX endpoint where the backend code delivers a result set ready for use in the frontend without needing to go back to the server again. Deliver the results from the endpoint in JSON format and then loop through the results in JS. A bit like what you’ve done now, but you’ll need to do additional processing in the backend code so the result set can be looped through in the JS without additional AJAX calls.

Way over my head, but will do some research. Thanks so much for the help!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.