开发者

Shall I use javascript for page submission?

I am working on a big site, and in the site there is a search module. Searching is done by using a a lot of user submi开发者_运维百科tted values, so in pagination I must pass all these data to the next page, appending the values to url make the url very big.

Sso how can I solve this issue? I am planning to use a javascript based page submission (POST) with all the values in hidden fields to the next page the read all the values from the next page.

Will it cause any problems? Or should I use database to keep the search criterias?


I would create a server side object, possibly with a database backend which is updated by the different pages.

It is at my opinion the most clear and easy solution. Giving parameters from page to page, either by post or javascript or cookie will work too but it's more of a quirk in my experience.

Also if a search query is so complex that it needs multiple pages to create it, it might be helpfull for the user to have all the data stored on the server so he can change it more easily by switching back and forth between the different pages.


I would store all the search criterias in some kind of session-store on the server when the initial search is being triggered.

For pagination I would retrieve the criterias from the session-store and then just show the appropriate results. Also I would append some kind of key to the pagination links (so this would be the only hidden post-field) under which the search criterieas can be found.

Even though the session is per user, you might have several search windows open within the same session, and you don't want to mess them up with the pagination.


In order to make a reliable search with pagination, we need to do a bit more than normal.

We need to handle the following cases.

  1. Once search is done, user may choose to do browser back and forward. Here, if you are doing form submission on every page, it would be an overload. Also, if user presses browser refresh button, it will unnecessarily warn him that data is being submitted.

  2. Searching on a large database with lots of criteria is costly. Hence, optimization is important.

So you should NOT do the following:

  1. Submit data on every page change
  2. Not store data in cookie. (This is not secure and not even reliable.)
  3. For large database with complex query, cache the result in session.
  4. In case, you need very up-to-date and real-time result, ignore point (3) and try doing partial search for every page.

Thus, for your case, you can do the following:

  1. When user searches first time, make the form POST data to a search page.
  2. This search page will store the search query in session and generate a unique id for it.
  3. Now render the result page. The result page will be passed the search id (generated in point 2) and the page number. Example result.aspx?searchId=5372947645&page=2
  4. The result page will puck up the query from session using the searchId and then provide result based on the page number sent.


Using hidden fields and POST method should be fine too unless you are able to get them on the next page right.


To supplement Sarfraz's answer...

It's not necessary to use Javascript to make a POST.

<form action="destination_url" method="POST">
    ...
</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜