Simple Web Questionnaire - php

I am making a website that will tell the user if they need a certain document in their business transaction. I know how to make forms, but I want to be able to ask the question on one page, and if the answer is yes, it goes to the next question- if no a different question. Like this:
Also in one of the questions, the user's name and other info are inputted. How can I hold onto that data and then put it on the correct document needed?

If you wanted to hold onto the data from a user input, you could use something similar to: localStorage.setItem("item-name", value); or sessionStorage.setItem("item-name", value);
when the form is submitted where value is the data from the user input. The difference between the two is localStorage keeps the data past a session while sessionStorage keeps the data until the user session expires.
Finally, when the data needs to be retrieved, you can use localStorage.getItem("item-name"); or sessionStorage.getItem("item-name");, depending on which one you used to store the data, and set the result in a variable to then be displayed in the final output.
I hope this helps you out!

Related

How to get website to remember choices to be inputed in to a form

I'm looking for a way to create a page with items on it where each item has a button which can be clicked to "like" or "choose" the item. The website would then remember the choices and input them to a form on another page. Is there anyway of doing this?
At first glance if we are talking about a registered user, i would create fields in the database for the user's input and store its value everytime he makes a selection. Then its only a matter of putting those values wherever you want.
You have several choices. If it is persistent data, I'd suggest a database like MySQL. If you don't have access to that, then Google for "flat file database" and you'll find some great ideas.
If the data is persistent, but not a big deal if it gets deleted or spoofed, you can use cookies.
If the data is fleeting (it goes away at the end of the visit), then use sessions.
The best way to do is storing each result in a SESSION and reusing it in other form.

PHP pages that store information between forms so that the form information is stored before submit

This is inside a PHP website form.
An example is like:
First page:
-Username:
-Password
Second page:
-Email
-[Checkbox]
Thirdpage:
-Contact Details
-Address
Fourth page:
Review all of the above forms in hard copy but with a back and forward command so that the user does not loose any information when switching between these pages.
Please post.
You could use cookies and do your own sessions with MySQL too. I like doing it like that because the data is easier to access if necessary.
Or you can pass the previous variables to the next page though hidden form elements.. but that can get messy.
You Have to use session. I like Zend_Session.
If you want users to be able to come back later and complete the form (assuming you have some kind of login system, or a way to recognize users), you could still save the data into a database. Make another table, temp_submissions. Keep data in it until the user completes the form and commits the data they send. Once committed, clear the data out of the temp_submissions folder and insert it into the "permanent" table. It may not be practical in this case, or total overkill, but it's an alternative to sessions and cookies.

saving form data to database

I am new to PHP, so I am looking for some input on how to make my project a little simpler.
I have a form in which a user can create a list of song, the Submit button then sends it to an intermediate page that saves it to a MySQL database for later use, the intermediate page then forwards them on to the final page that shows them the lyrics of the songs in the order that they chose them.
Originally I had the intermediate page and the final page combined, but ever time a user refreshed the page it would resubmit the data to the DB. What I have works, but it seems like there should be an easier way to accomplish this.
#micahmills: An easier way of stopping duplicate data from being added to the database? Well, it would depend on what you'd consider "easier" -- less code? Less steps? Something else?
What you could do is generate a unique hash or token that submits with the form. This token is then stored in a session after successfully inserting to the database. Attempts to repost the form will then fail because the token sent with the form will be the same as the one stored in the session.
Redirecting to another page after posting to the database is still one of the best and simplest ways of preventing duplicate data being posted though.
Best practice is to redirect after database operation to success / failure page.
You can have form & intermediate combined and a final success page, on failure you need to return back form.

Session questions

I am having trouble in understanding of how I need to do this:
Have list of items on my home page and when users click on that, I take them to a page where i ask for name and email id(both are optional fields) and when users hit submit, I will take them to other page where they get to see all the details. the reason for that name and emails fields are to store the ip address in mysql and also the url(has the item id and date) they are on.
To do this, i am having trouble in doing it in program.
I am thinking to start a session/store cookie once they hit submit. after that I want to store the ip address, item id, date and name/email(if they filled in) in mysql db
Store everything in the mysql db
Also, how can I avoid anyone going to the page directly(the one I show after they submit) ? Is there any way can I include some condition on that page and redirect them to this log in page ?
Please help me out.
regards
Since you set session variables when the user hits the submit buttons, you can test if one of those variables is set or not and redirect accordingly.
You can also do it with POST, use the page as an action to your form, and whenever someone accesses that page you test if $_POST variables (from the form) are set or not.
As the data seem to be necessary only for the immediate use, I think that a session is the right answer in this case.
If you would then use a database query, which data would you store to associate the data to the correct user? As you said, both the data you ask are optional; even in the case there would not be optional, how do you handle the case two different users report the same name and email (it could also be the same user using two different browsers).
For temporary data like that, the session is always the better choice (except few exceptions, maybe).
I was forgetting the other question.
Also in that case, I would use a session variable. Sessions variables are the solution for values that you want to keep between different forms, without the need to keep moving them between the server, and the client side.
For more information about sessions, see PHP: Sessions

PHP, MySQL: How to retain and retrieve correct data when dealing with pagination

I have a form (that consists of 25+ fields) and the values for these fields, range from a tiny value to a concatenated string. This is like a search tool. Now when the user fills the form and submits the info, he gets to see all the relevant data that matches the criteria in the form. I am displaying 15 records at a time to the user. I have implemented pagination to enable user to see the other records as well.
THE MAIN PROBLEM:
The part, till the users submits the info and gets back the 1st set of data is good. The problem arises when user tries to go to 2nd page (or any page of his choice) via pagination. The user is able to navigate to the other pages but the query that is needed to execute properly for pulling out the results from the DB is not triggered. Notice that initally it was a POST operation that was performed in the form and the pagination performs a GET operation. So I am losing the values of the form that the user has input and I want to retain these values and query the DB with these values.
I am trying to avoid sending the form field values via GET because I fear that the data may exceed the maximum permissible value in the URL (& as it is less secure than a POST operation). There are other operations that can be performed on the results page that can lead to loss of the form values if I try to use a POST operation (like update query). Sessions would not really work as the user can choose to run the same form in different tabs with different inputs to compare the results and this can lead to the data of the older query replaced by the data from the newer query. I haven't thought of cookies as the user may have chosen to block it. Pretty much all the options seem to be exhausted.
So what can I do to retain the form values, run the proper query and get back the relevant values irrespective of the number of times the same form may be processed by the same user in different browser tabs/windows, without using sessions(given the restrictions on passing data via GET and possibly losing them in POST operations) and be able to perform other activities on the page as well?
Thank you in advance.
First off, GET is no "less" secure than POST. Both are not to be trusted at all (It's more inviting to modify a url string, but by no means harder)...
You have a few options:
One, would be to keep a global "store" of search results. You can use a db table with id, data where data is a serialized array of the variables. Then when someone submits a search, check to see if the data is in the table. If so, use that id. If not, then serialize the data and insert it (and get the id). Then redirect to a page like: results.php?id=4. That way, they can share the link yet it stays reasonably secure from tampering (they can't alter the search params). The downside here, is that the table could grow HUGE.
Another would be to base64 encode the data and pass that through as a get parameter (base64_encode(serialize($data));). I would try to stay away from this if you're concerned with tampering or url length.
Another solution would be to intercept the next link click in JS, and use it to issue a POST back to your server from hidden variables.
EDIT: Removed the session solution. Realized that it wouldn't work for your problem.
I am trying to avoid sending the form
field values via GET because I fear
that the data may exceed the maximum
permissible value in the URL
Then you're going to have to do your pagination with a form as well, so they can also POST. Either that, or you'll have to store query terms on the server side somewhere, possibly in a session -- but don't forget that a user might have multiple tabs open at once, so you need to be able to store more than query per user.
But there's no reason you can't store multiple queries in a single session, e.g. $_SESSION['queries'][1234]. Then your pagination links would look like ?query=1234&page=3
Consider, however, that it may be useful for users to be able to share URLs of their search results. e.g., if google used POST exclusively, I couldn't send you a link to http://google.com/search?q=somequery
(& as it is less secure than a POST operation)
Not really.
A possible approach would be to store the whole query you used for searching in your database in a table say called search_queries. That table should contain essentially two columns, a hash and the query used for that search item.
When a user submits a search form, his query is evaluated and inserted in that table and he is redirected to a page with his search_hash. Every time he navigates to a different page his hash is pulled from the database and results re-evaluated accordingly -- with the proper LIMIT of course.
Make sure you cron that table (for that you might need a timestamp for each search item)
Another viable implementation of this approach would be to store the query in a SESSION variable and use it for your querying purpose. For pagination, you would /search?page=1 and your _SESSION['query'] would be for example "SELECT * FROM Topics WHERE title LIKE '%test%'". You would essentially add "LIMIT "+($page*$perpage)+", $perpage"
However the latter approach would not be able to detect multi-windows this user has with the site. You could use an array in your _SESSION['queries'] and have the user submit a /search?id=0&page=1 where id would represent which query of the array you're querying in that window.

Categories