Handling search parameters URL PHP - javascript

I am wondering if I would be able to get some advice.
I am trying to build a search for my site and I want to link to be reusable so it can be sent as a link to someone else to see the same results.
The problem I have is that there is the possibility to have 100s of parameters for this search, so I dont think a GET requests in the URL are the right way to go.
I was thinking of the possibility of creating a JSON file that saves the parameters in the search and to give it a specific name say "qwer-eweq-qwe" then the URL link could be www.mysite.com/qwer-eweq-qwe
Then once a user would navigate to the URL it would then read the JSON file and pull the correct parameters.
Is there any other ways I could do this? Can seem to find too much online. any pointers would be very helpful
Thanks,
Richard

With so many parameters, please, consider switching way of dealing with it.
An idea would be to use a filter system. One POST method would create a filter with a set of parameters in body (either raw or JSON, does not matter).
POST /filter
The call would create (or not, if already exists) a filter.
The call would return a filter ID that you can use to search by fixed parameters:
GET /search/:filterid
That way, you even can manage cache limitations depending on filters, by invalidating those if needed.

Related

What should be the new URL content when using Javascript History API?

I am using AJAX to load content dynamically, in my page. (basically I have a search bar, and the search results are obtained using AJAX).
I want to make use of History API, since, After several searches(and also applying sorting and filter), if the user press browser back button, it takes them directly out of my site, instead of taking them to the previous search result.
There are several online resources about using history API, but I could find any information regarding the URL, that needs to be set using
window.history.pushState(dataObj, title, URL);
If we just set any URL maybe like www.mypage.com/value1, then, this would become a fake URL, which if bookmarked and used later, will not work.
So I was thinking of using the following appoach.
1)Set the URL to www.mypage.com?variable1=value1&variable2=value2 .....
Where value1 and value2 ... would be some information, which I use for AJAX.(In my case these values would be the search text and the filter and sort information)
2)On press of back button, retrieve the URL, spit it using "?" and "&" separators, and take action accordingly
3)If this URL is bookmarked also, I can handle it appropriately, since all the information is available as variables.
So, My question is, Is this a right approach, or is there any better(standard) way of handling such scenario ?
This is somewhat opinionated, but I think your approach sounds fine. So long as the user is able to press the back button and directly visit the URL to get the same results, then I think it is a perfectly fine way of doing it, and I cannot necessarily think of a better way.
In fact, from what I could grasp after looking into the Google search javascript code, it seems like they use a very similar method. They use history.pushState(...) to add search query URLs to browser history, and when they initialize the window, they check the history object as well as the URL to decide what search results to load using JS. That is not to say that Google is the go-to guru for proper web development, but if they are using a method like that, I think it is pretty safe to say that you would be fine to use it too.
I would definitely be interested if anyone has a better way, because that seems to be the best.

Define in Smarty the format to be returned by AJAX

I have a html form in a Smarty template. A select in the form is populated via AJAX. I would like to specify in the Smarty template, the format to be returned by the AJAX-call. This way I can use the same request-url for different purposes. For example if the select contains users, sometimes I might be interested in knowing the user's email address (format: "{$Option->GetName()} ({$Option->Email})"), and other times the user's organisation (format: "{$Option->GetName()} ({$Option->Organisation})").
I have a few ideas of how to achieve this:
Store the format in html/js in the template. Then the AJAX-call can send the desired return-format along with the request. Drawback: A malicious user can change the format in the request, which might be a security issue.
Pass along the entire $Option object, and do the formatting using JavaScript. Drawback: Cannot use the PHP object's methods, such as $Option->GetName().
Store the format in a separate template file, and reference to this file in html/js and send along with the AJAX request. Drawback: The reference to the template file can be changed by a malicious user, but probably no damage can be done. This approach requires extra template files, and the presentation logic will be spread over different files.
Create a Smarty plugin that automates the process of item 3. Then the presentation logic will not be separated. Drawback: The reference to the template file can be changed by a malicious user, but probably no damage can be done.
Do anyone have any other/better ideas, or any experience with doing something like this?
I don't think you need to set some format. You can create a javascript function with 3 parameters. The <select> tag identifier, the name of the key used for options value, and the key name used for options text. The function will make the ajax request. That way you can call the function with settings you need for particular situation.
Further details depend on your current code.
I ended up using option 4, and I'm very happy with the result.
If anyone come across this and need information on how I implemented it, just post a comment, and I'll try to explain.

SQL Data & JavaScript - Best Practice

Let's say a website needs to pull information from a specific table in a database based on a user's menu selection. That table's data is then fed into some JS equations and thrown onto the page.
What is the best way to go about pulling that table's information? I've read that trying to access an SQL database via JavaScript is bad practice, so is there another way to do this? I know about PHP's json_encode, but I guess I'm not entirely sure
What the syntax is if I'm calling PHP from a JS script, and
If that's 'best' practice. Still relatively new to this, so I'd like to do this right.
Another option as far as I'm concerned is attempting to pull ALL of the possible tables (not a security concern) at once on page load. I expect that'd introduce a good deal of latency, though.
It looks to me that you are not really sure what technique to use. Here are some options. I'm not going to type them here because, there is enough to find about each one:
plain php: w3schools
pure ajax call: stackoverflow
jquery: jquery
Ajax calls are more user friendly and many times more efficient because, you don't have to refresh the page. I usually get all information at once( as long your mysql data is not to big). As for security: You use php either way so it doesn't matter if you use Ajax or not. Oh and don't select valuable data of users data (like password or their emails). I hope you get more overview after this :)

What does the collection returned by CrmRestKit.RetrieveMultiple look like?

In a form in CRM2011 I am using a JavaScript function to retrieve some attributes from a custom entity unrelated to the one in the form.
I have a successful call to CrmRestKit.RetrieveMultiple but I don't know what the returned collection comprises. Can someone point me in the right direction, please?
To be a little more specific about the requirement: the query returns a set of Field schema names; i.e. the column being queried is in a custom entity and contains schema names of Fields. I want to match each one I retrieve against the calling form's collection of Field-based controls so that I can perform an action on matching ones. Any assistance towards that would also be gratefully received, thanks.
The easiest way I have found to know what you'll be working with is to take the output and run it through JSON.stringify() and write the contents of that out to the page.
For bits like this I usually just debug with IE. That will allow you to add breakpoints and inspect the object.
Related info: Debugging Script with the Developer Tools.

Pass GET variables without the URL?

Suppose I have these two links on my homepage
http://example.com/user?id=1
http://example.com/user?id=1&sex=male
These two links point to the same page. The users page loads information based on the id.
If the second link is clicked the page loads extra information based on sex.
These two pages are almost identical, therefore not good for SEO.
My question is it possible to pass the second argument without using the url?
Maybe using POST or javascript?
Thanks
Using POST is the answear. Even though I have no idea, how this influences any SEO strategies...
The only other way to pass it without GET would be POST or by using cookies. They may be other methods that I am missing, and please correct my if I am wrong.

Categories