How to store the value? - php

I want to make the application like below.
First there is a page with four radio buttons. The user clicks on any of the radio buttons but doesn't save and he goes to another page. Then if he comes back to same page with the radio buttons it shows the selected radio button before he goes to another page.
How can I do it by using jQuery and PHP?

You can either save the value in a database, or in a cookie (with javascript, jquery: settings cookie with jquery
EDIT: As palasso said, you can also use php sessions instead of the cookie, all depends on your need.
For example, the user can change the cookies you created (they are just .txt files) and alter the information within.
In your situation, this dont seem to be a problem (becose the cookie depends on the checkboxes the user clicked).
So, you should go with database storage if:
Need to store the checks for long time;
Need to do complex elaboration on them;
Else, go with cookies/session.

First of all you have to start session in your php script. Then use ajax request on a radio button click storing the selected value in the session.

Sessions and cookies are the easier options. I prefer sessions.
Add a click event to the radio button
When clicked save the selected option into a php session
Each time the page is loaded check if the selected value exists in your session and load it.

Related

how to retain the selected values in the form even after page refreshing

actually i'm new to php & i'm developing a project SIMILAR TO ONLINE EXAMINTAION.
so i'm display questionS RANDOMLY FROM D.B
what the thing is.
i'm displaying 5 questions ALONG WITH THEIR OPTIONS in a single form.
and i selected a radio button from a form & done page refreshing.the selected radio button can visible like unchecked after the page refreshing.so i want to display a radio button as checked which was selected by a user before page refreshing..is it possible.
or can u suggest a code to block the browser refresh button...
Try the sisyphus http://simsalabim.github.com/sisyphus/ - nice and easy to use JQuery plugin
You can store selected values in the cookies using javascript and on load event read cookies back and populate the values back.

How to use PHP/jQuery to realize a unique user on load?

I have a webpage that stores the user ID of someone who clicks a button on the page. Once the button is clicked, that user needs to be able to see content (just another button) that only that user can see. Other users on that page won't be able to see that button.
The problem I'm having is I don't know how to use that unique user ID, which is superglobal, to recognize on load what the user ID is, and how to execute if its the correct user, show the otherwise hidden inputs.
It is same problem that arised to me
You have two option to solve this problem
use a PHP globals like $_COOKIE[] or $_SESSION{]; To store the id of that user
or you can use a database field of array type.It will store the value of button clicked by a particular user(using a database will more efficient because you can use it any time and it do not expires )

PHP Sessions and have "prev" and "next" button and retain values

I have a multi-page form.
I'd like to have a "prev" and "next" button on page 2, 3 and 4 so that if an user in the middle of filling out page 2 can decide to return to page 1 to edit/enter something and be able to still see values on page 2 where he left off.
Is this possible? any pointers/links would be immensely helpful.
(Yes, I have sessions and I can see the sessions after printr but if Im in the middle of page 2 and click backwards, I still dont see these sessions).
Thanks in advance.
Start the session when the user hits page 1. Each page should look in the session to see if there's any data there for that page, and if so, render the form with those values pre-filled. When the user hits either PREV or NEXT, process the form as if they hit submit, but instead of saving the values to (for example) a database, save them to the session. Then redirect to the requested NEXT/PREV page. When the user hits DONE (or whatever) on the last page, pull all the values from the session and process them to your database (or whatever.)
For a simple approach to a "multi-step" form, simply use Javascript. That is what I do.
However, if you want to have a stateful form that remembers data in between pages, you will have to use a session array to track the values entered.
When you fill out a page, the POST array is populated on page2. Serialize the array of post data and store it in a session array. Back on page1, if that session array is set, unserialize the data and echo the values into the right form element.
You can save all of the data in a database as they move to a different page as well as print these values out when the page is loaded. This would still have to be sent to the server via AJAX in the case of a "Previous" link click.
You can put all pages in one html page, then just hide/show the correct pages when they navigate using javascript.
Save all entered data in a session, which would have to be sent to the server via AJAX if it is not submitted through a form the traditional way.

'Previous' Button in HTML/PHP

I thought hours about that problem but I didn't come to any conclusion. My problem here is that I need a 'Previous' Button added to a form. The user should fill out a formular that is splitted up in 13 parts. Every part is an own formular having a 'Next' button for submitting everything to a database and redirecting to the next page.
How do I integrate a 'Previous' button there? ...
I don't if it might be usefull for you to know that I'm using cakePHP, and well I'm pretty new to it.
Store the POST data of each form and the current form index in your session.
When clicking the back button, open form (currentForm - 1) (if that's a valid form index) and populate the fields with formData[currentForm] (assuming currentForm is now the form the back button redirected to)
The question really is, do you want to store each stage of the formula in a record? or do you want to store every stage of the formula in the "transaction"? The difference here is important. What is your relationship with the user? Do they login? are they anonymous? How do you associate their answers from one form entry to the next? If you store each entry in the database, in some chronological way, then simply populate the previous form with the previously entered values; when they click previous. If you do not store the entries and instead utilize a session to retain values between "next" clicks then populate the "previous" form with those values.
I've coded a similar form in classical ASP, see if you can make it work in CakePHP:
I had a 7 step form, step 2-7 have previous buttons. Each step consists of one asp script. All scripts post back to themself. I check the REQUEST_METHOD upon every invocation of the script to see if it was called by GET method or POST. If POST then data is validated, if validated then it is saved. There are three submit buttons on forms that allows user to choose whether he wants to just save the data, save and move to next step or save and move to previous step. Depending on which button was clicked, the user is "redirected" to the previous/next page. This post specifies how to add and handle the previous/next buttons:
Multiple Submit Buttons — Specifying default button

Php, passing data between pages without using the url?

I have a php page that has a form that asks for an e-mail. When you press the send button, it gets to another php page, which gets the form data and does its stuff. I need to then be able to go back to the old page (the one that contained the form) and give it some data so that it will be able to change itself and say "You've sent your e-mail successfully, and will not display the form.
How do I do it?
Sessions probably
http://us2.php.net/manual/en/book.session.php
You can either use sessions or cookies, to not depend on the URL cookies have always to be enabled.
Check the PHP Manual (Sessions and Cookies).
Options:
1) Set a cookie (or use a session variable, which is kind of the same thing)
2) Use a separate thank-you page. After you've processed the form, redirect to http://www.mysite.com/thankyou
3) Process the form on the same page as itself. If your form is at http://www.mysite.com/myform, then at the top of that page have a little
if ($_POST)
// process form
// display thank you
else
// display form
Good luck!
If the user is just seeing data that they've entered anyway, you can just use hidden form fields:
<input type="hidden" id="lang" name="lang" value="en" />
That way you can continue to POST new forms and pass the data down the lane. That's the easiest thing to do without having to write a single extra line of PHP code.
You could also store each section in a database and save each section as-added. That would give you the added benefit of having partial data in the case of a browser crash or whatever, depending on how many parts your form is. You could then pass just an ID to the DB table row and retrieve the data for display.

Categories