On a site that I'm working on, I'm displaying results from a database query. Each page shows 15 results, so if there are 45 results returned from the query, they will be displayed on 3 separate pages, each containing 15 results.
I want the user to be able to select any number of items from the first page, then switch to the other pages and select items from those pages as well, while still remembering the items that were selected on the first page. (Eventually, when the user has selected all the items, he will click a Submit button.) So if the user navigates back to the first page, the items that he checked should show up as being selected. On pages that I've seen on the Web, when the user navigates back to the first page, all the checkboxes are cleared again (which is not what I want).
Any ideas on the best way to achieve this? Thanks.
Remembering the selections in-between pages means you have some state that needs storing somewhere. There are a variety of ways to do it, but it boils down to one of these...
Keep it on the client (e.g. cookies)
Keep it on the server (e.g. in a database)
Keep sending it backwards and forwards (e.g. hidden form field, as in ASP.NET viewstate)
A common abstraction offered by platforms such as PHP and ASP.NET is the idea of "session". The actual implementation of session state might be some combination of those 3 possibilities above, and it probably offers the easiest route. I suggest you look at session state in PHP.
use hidden inputs to carry over the results from the previous set
<!-- hidden fields -->
<input type='hidden' name='whatever[]' value='whatever'/>
<!-- checkboxes -->
<input type='checkbox' name='whatever[]' value='whatever'/>
You could do many things.
Save as a JavaScript cookie
Save as a session by using AJAX on each click
Hide each page as a "tab" so they are actually there but just not shown.
Related
I'am using the CodeIgniter. I have a global category list, which is separated into the several components such as (Conten articles, E-commerce, Users, Banners, etc...). There is /categories/get_categories page where I display all rows from Database Table ci_categories.
On that page, there is a <select> box with the <options> of available ci_categories.com_id (components). Whenever I select one of them, either Users or E-commerce it will send the POST data /categories/get_categories/$com_id and it will filter the category items corresponding to the component's id com_id. All of this works great.
But what I want is to keep this filter selected and do not return to default selection (all components). For example, I click on Categories - it will output all category rows in a <table>. There I choose an option from a dropdown selectbox list, and it will filter the specific rows output, then i Click Add new category, and it should pass that selected component option to the next page. On the next FORM page, where I add a new item informations, i Click on SAVE, and it should return me to the previous page where All of categories are listed, but with that filter com_id selected.
Any suggestion ? Is there a way to do it without sessions or cookies ?
My suggestion is to make use of sessions as they are especially meant to store data between requests based on the current user experience. Most people only search the website in one browser window, so it shouldn't give a real problem. Even if there are other options, you always need to identify the user by a specific code which will be stored in session/cookie.
The only option I can think of is changing every link afterwards to contain the stored information in a base64 encoded string. But that would ruin your link structure and needs canonical links on every page to show the right url to search engines.
I would suggest you to stick to Sessions, as they are handled fine in CodeIgniter. You can even store them in the database if you want by setting that in the main config file.
Flashdata is an option, but it's a session in the end anyway but only lives one request.
you can use session flashdata like this:
Set flashdata
$this->session->set_flashdata('search','your_search');
Read flashdata
$this->session->flashdata('search');
flashdata is setted for only one refresh, after the refresh the variable is deleted
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
I need store different post data to each tab in a browser. If I open a new instance of the same page, the data aren't shared between both.
My problem:
I'm building a CMS to control my website content. But I will open some instances of the same page (many tabs). So I have a search form to find news that I been created on my CMS. If I open a news item I have a cancel button that back to previous page (the news list).
The problem is that the news list have a pagination and a filter form. So I can, for instance, search by a term like "john doe" and advance to page 5, and open a news item. If I cancel, currently I back to news list without filter and on first page.
My solutions:
Well, I don't want to use the history.back() because I can submit a news form and click on back/cancel button. So, I'll back to the current form, what is wrong.
My second idea is to store a $_SESSION with the $_POST sent to the news list and the back button send me to /news/list/recovery-session, that will recovery the $_POST data from session. But it have a problem: if I open two tabs and make two searchs, I'll have only the last session saved.
Your solutions:
Well, I can work with PHP and JS to make it work. You can suggest a idea of what I can do. I think about work with COOKIES, but I belive that it is shared by domain, and not by tab, what is a problem.
Someone?
Generate a unique id and attach it to the form or some hidden element that will be submitted. Save that unique id in a cookie or session variable. Compare the two at time of submission.
If second tab has generated a new id, the first tab will not evaluate to true.
The main problem is the need to persist the state of what page to return to and also the search term when returning back to the news list page. There are many ways to accomplish this, but one simple method is by encoding this data into your query string.
As an example, assuming your search term is "john doe" and you are on page 5, pass that data along to your news page.
news.php?returnSearchTerm=john+doe&returnPage=5& ....
When the news page is created, you can format your cancel link to send you back to the list page with the correct parameters.
news_list.php?search=john+doe&page=5
How do I create a form that spans over multiple pages? I would like to ask a large amount of questions, and based on the answers of previous questions, new specific questions will be asked.
Traditional method: store all the previous answers to questions in <input type="hidden"> fields on subsequent pages, so that the last form contains the entire submission (only not all visible). Drawback: file uploads are annoying to remember, amount of data can get large.
Common method: throw answers-so-far into the session or cookies as you go along, picking them out at the end. Drawback: concurrent operations end up with unpredictable conflicting results. File uploads still annoying. I would avoid cookie/session storage solutions for this.
Modern method: have a simple (long) single form, but use JavaScript to show only part of it at once. On clicking ‘Next’ you simply hide the previous page worth of content and show the next. The last button is the real submit button.
There are several ways to do this:
Incrementally populate a database, submit by submit
Keep all form fields on every page, but hide (<input type='hidden' ...>) the ones that shouldn't show up (as #Trufa suggests in another answer)
Accumulate an object that's persisted implicitly in the session (depends on your server architecture)
Accumulate stuff in one or more cookies (seems icky by I list it for completeness)
If you wanted to be super-trendy, you could accumulate information in HTML5 client-side storage. Of course that'd rule out older Microsoft browsers.
There isn't such a thing as a single form spanning multiple pages: each page/form is independent of the others.
If you somehow keep track of what has already been entered (possibly in session variables), then you can just display the new questions in a new form until you have all the data you want.
I'm assuming that by your question, as there is no such thing as a multiple page form, you mean how do you build up a set of answers from a user by using multiple pages, each containing a form.
The mechanism PHP supplies for such things is the session mechanism. You'd build up the set of answers in the session as the user submits each page, and when you have a complete set the full set of answers in the session gets committed to the database.
This isn't the only approach of course, you could also incrementally add the data to the database on each page submit, but then you will end up with partial answer sets where people have only done the first page or first few pages and then abandoned it. You'd have to write code to handle these partial submissions.
I am trying to develop a registration page which involves three separate information.
First Page will get contact details
Second page - working details
Third page - study details.
How to keep the form data of the previous pages before posting the form?
You could do it with Ajax - multiple divs and hide/show the appropriate ones.
Or you could POST each page and save the data in the $_SESSION global variable until all pages are complete. Then save it all to the database.
While the other answers are certainly good ideas, you may also want to consider persisting the intermediate data to your database between each page. So, submitting the first page would create the new row, with the columns relating to contact details populated, and a status column set to a value indicating that the submission is not yet complete.
The second page would update that record in the database. The third page would also update the record, as well as the status flag to indicate the submission is complete.
The main benefit to this is that the user can walk away after the first (or second) page, and then return to it later, even if he had closed his browser and his session had expired. (As long as he has a unique URL to return).
This approach might not have a lot of benefit if you are only collecting three pages of data, but if you had many pages, the ability to leave and return later might be more important.
You should take a look at http://jqueryui.com/demos/tabs/, it should be able to do what you need.
While shifting to another page, you just put the values of first page variable in sessions, then you can access the value of previous page at any page, then post the value to the database query. In this way, you can use the use the value of first page at third page, up to when browser is open. As the browser close then variable lost their values.
Back in the day, I would've put hidden fields for all of the previous pages in each subsequent page, so the final submit would have everything... i.e.
Now, I would probably only have one actual page.. with multiple steps implemented by showing/hiding div's and collecting all of the data in one big form, broken up visually for the user... and if I was feeling especially frisky, with frequent validation and final submission through ajax.
I'm looking to create a multi page ordering form the first page would contain some dropdown and text fields, the second page would contain more text fields, the third page would be an order summery with paypal payment option.
I'm just wondering what the best way to create this order form is. I've used sessions in the past but never with users entering in text and picking items from drop downs, does anyone have any resources for doing this? Or does anyone know of a jquery or other ajax example or plugin I might be able to use and modify.
any insight would be a big help.
thanks
The simplest technique might be to use hidden form fields to carry fields from previous screens through to the final screen.
Just make sure you validate all the values when the final screen is submitted to make sure that the user hasn't twiddled the data.
You don't need to do pagination at all if you don't won't to. Just use css to show/hide the "pages". It doesn't sound like you have to save the "state" at any point.
But if you want to do multiple pages, use a session or a cookie to track the user. Then save the data to a database a mark it as incomplete. On the final page, retrieve it all and show it on the page. The server can't tell if a request is ajax or not, so it doesn't matter what you use for submission.