4

I am trying to pull in some Low Search results using AJAX. How can I pass a query to the result page without knowing the encrypted version of the keywords beforehand?

Here are some things I have tried.

I placed this on my result page in hopes that it would give me a URL I could use.

{exp:low_search:url
keywords="{segment_3}"
encode="no"
result_page="/my/result_page/"
}

The URL Tag spits out something like this:

http://mysite.com/?ACT=21&keywords=eyJjb2xsZWN0aW9uIjoiY3VzdG9tZXJfY29udHJhY3RzIiwibG9vc2VfZW5kcyI6InllcyIsInJlc3VsdF9wYWdlIjoiY29udHJhY3RzXC9zZWFyY2giLCJzZWFyY2hfbW9kZSI6ImFsbCIsImtleXdvcmRzIjoibHluIn0&result_page=%2Fmy%2Fresult_page%2F

I was hoping that it would not encode the keywords, and I would simply use a similar pattern for my AJAX call.

The URL it generates does not work as a stand-alone, therefor will not work for the AJAX call either. I have ruled out AJAX as being the problem, and come to the conclusion that I simply do not know how to pass keywords to Low Search without using the standard (non-js) form submit.

Any ideas?

Link to my question with Low.

1
  • I have answered my own question, but can't post for another 7 hours. Once this time slot has ran it's course, I will be back with my solution.
    – Jarrod
    Dec 20, 2012 at 20:55

3 Answers 3

4

The main problem here is this:

keywords="{segment_3}"

You probably don't have keywords in the third segment, but an entire encoded query. You're feeding that query as keywords to the URL tag, essentially encoding it twice.

If you use the URL tag and you want to work from an existing search action, use query="{segement_3}" to pass all the details from the existing query on to the URL. Then use any other parameters to override those.

For example, say you've searched for foobar using the Low Search form. That will redirect you to something like domain.com/search/results/xxxxx, where the segment 3 is the encoded query (not just the keywords!). Say you want to generate a link from that query to something else:

{exp:low_search:url
    query="{segment_3}"
    keywords="barfoo"
    result_page="other/template"
}

This will generate a URL like domain.com/other/template/yyyyy, using barfoo as keywords, but maintaining all other details given in the first query, like Collection, Search mode, etc. This link will not add the search to the log. To do that, add the encode="no" parameter:

{exp:low_search:url
    query="{segment_3}"
    keywords="barfoo"
    result_page="other/template"
    encode="no"
}

Which should give you something like domain.com/?ACT=21&keywords=barfoo&result_page=other%2Ftemplate.... This will add the search to the log.

1

Low really is the best person to help you troubleshoot this but I'm guessing you're in a hurry, so in the meantime...

Could you bounce your search queries off a template containing something like the above, and then use a redirect inside that to actually return the search results to your request?

{redirect="{exp:low_search:url keywords="{segment_3}" result_page="/my/result_page/"}"}

That way you'd get a predictable URL structure to target with your ajax request, even if you can't decode the query string.

If you look at the release notes for v2.0 it at least suggests that something like this should be possible, though I may well not have the syntax right:

2.0.0 Improved compatibility with Ajax form submissions and EE's native redirect method

0

I accepted Low's solution as correct, but I have implemented in this way:

I assumed my AJAX was good, because it worked just fine when submitting a URL that had already been encoded by Low Search. Instead of using POST method, I used GET method to send JSON 'keywords': 'my search term'.

Here is a piece of the javascript puzzle stripped down for readability.

Here is what my result_page template looks like. Keep in mind, I am posting this answer before it is completely ready to ship. I have not included proper JSON handling if there are no results returned.

I have tried to make this a plug n' play as possible, but it certainly won't be. Actually, it's likely this isn't the best solution. I will come back if I find something I did very wrong, or if there is a simpler solution, etc.

Please feel free to tear it apart if you can offer something better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.