Using a class on a multi-page form - php

I would like to use Classes in my next website. Part of the site involves a multi-page profile form. The visitor fills in their name, submits this and then fills in their date of birth, submits this and then adds some text.
When the first form is submitted I will instantiate the class and update the database etc but when the visitor submits the next form do I need to declare another "new" class or does it stay instantiated?
What is the recommended way of achieving this kind of behaviour (ie not using classes for this / using sessions to hold the data / instantiating all classes on each page refresh / etc)
Thanks for some direction with this.

As PHP is stateless any classes you initiate only exist for that one page. If you create an instance with data in, it will only exist on that page. If you want it to exist on multiple pages then you will need to reload it on every page.
For what you're describing it would seem that you would first gather all the data, storing the users answers in session between each page, and then once you have all the answers initiate your class to do whatever it is you want to with your data.

Php data is unset after next request exept for persistant data like session and etc... .
To answer your question:
If this case is on one request and the scope of the variable is ok you could use the same variable(and instance) of the object.
If not, you should make a new instance.

Related

Simple Web Questionnaire

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!

CakePHP highly integrated SignUp Form

I just wonder how to build up a signup form like this with cakephp 2.x:
A member can have multiple workingtimes and multiple vacationtimes.
There should be one large signup form with one submit button and 3 visually separated areas: memberdetails, workingtimes, vacationtimes.
During signup a member should enter his personal data inside memberdetails and add arbitrary workingtimes as well as arbitrary vacationtimes in the designated areas before hitting the submit button. All the entered times should be listed tabular inside the form before submitting the whole form. At some point the memberdetails, workingtimes and vacationtimes look good and the user submits the form(I know that this is only the ideal situation and there are missing some functions).
My first approach was including the forms for adding workingtimes and vacationtimes inside the members form and send serialized form data via Ajax-POST to the other controller actions (Workingtime->add, Vacationtime->add) and also load the whole related data with something like a crud index function via Ajax and inject the response into the signup form without reloading it. This seems to keep things like validation, security component or the view layout simple, because most of the work will be done by cakephp and not with javascript, but I think it only works if I have allready a database ID for the member - after this I can store workingtimes and vacationtimes. Like allready said, I would prefer one form with one submit button and be able to cancel the whole registration even if there where allready added workingtimes or vacationtimes.
So what is the cake way to achive an integrated form like this? Is it useful to start a transaction and create a dummy member when the form is loading? And then use the ID of this dummy record to store the related workingtimes and vacationtimes? And when hitting the submit button the dummy member is updated to store the entered personal data and everything can be commited? Or should the entered related model data only be validated without saving and then just cache these data for a final transaction block?
Any other ideas?
I think the best approach for this is to manage states. Allow user to enter the basic data, change state, and then you can ask for more information but you will have already a user id to associate and match info. If the user comes back later it will have the current state already.
With Cake, a Component would help you to go from one step to another. Each data you enter should be validated.

Form processing using two php scripts one after another

I have a page with a form for logging to a sub section in my website "login.php". I want to do some logging of the user information. Like Username, time, browser used... but without touching to the original 'login.php' with a certain transparency to the user, the pass the form to the original script for setting cookies...
I was thinking adding a second page 'login.html' containing the form with action=logtofile.php then pass the data to the original 'login.php' that allows the user accessing to the members area.
Try being more specific, I've no idea what is your problem. Also there should be at least one question mark in a question :)
I don't see why you need two PHP scripts, why can't you write both logging user information and perform user login into one script?

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.

How do I control flow of PHP multi-page form?

I am a bit of a PHP newb
I have developed a multi-page form which works fine at the moment - each stage is on another page (I use the session to retain the data).
However I know that users don't always use these forms the way you want!
I want to control the flow of the form.
I would like the user to be able to use the browser back & forward button for ease of use.
They should not be able to skip a part of the form by entering a form stage URL directly into the address bar to get the a later stage in the form (essentially skipping a part of the form).
The form also does not flow the same path every time, it is dependant on the users choices what stage is displayed next.
I was wondering if anyone had any ideas of ways to control the flow of this multi-page form thank you!
store form results in SESSIONS (encrypt them if sensitive)
then just check on each form if the value is set and show it as necessary.
use another session to check the "progress" of the form, to prevent the user from skipping ahead.
for example...
<?php
/* on form 3 */
if(isset($_SESSION['progress'] && $_SESSION['progress']==2)
{
//the second form has been filled out and validates
}
else
{
// the 2nd form hasn't been finished, redirect
}
?>
you could also use like a percentage based system in the session - a value of 90 means that 90% of the form fields have been completed - for displaying "progress" in a visual means to the user.
basically on every form submission, check whats been submitted, if its expected, then set appropiate sessions to redirect to the next stage.
check every set session on every form to determine if the user should be here yet.
Push the data for the non-current fields into a hidden field in the browser (to save time and effort - just serialize an array/object).
I would like the user to be able to use the browser back & forward button
If users are allowed to re-enter previous stages, just let them and rewrite current stage in the session.
If not, make form fields read-only and do not process submitted forms for the previous stages.
That's the only problem I can see here.
You can either use session data to retain the state between multiple pages, or you can transfer all data on each page. Typically you would do the latter with hidden fields or you will create one humonguous form, and use javascript to make it appear as if it was multiple pages, when - in fact - it's not.
There are pros and cons to each solution.

Categories