Click to See Complete Forum and Search --> : Creating a multipart form that were users can move back pages without losing data


pauladkins1974
11-17-2006, 01:21 PM
Hello All

I'm currently using this code to pass data from one page of my multi-page form to the next, which is fine.

<input type="hidden" name="type" value="<?php echo $_POST['type'];?>">

However I'd like to take it to the next level so that users can hop back and forwards through pages within the form without having to re-enter any data.

I suspect I need to construct an array to hold my data but don't know where to start.

Does anyone have a script that can do this or point me in the right direction?

Thanks

Paul

so_is_this
11-17-2006, 02:15 PM
This is easily accomplished using a PHP session and $_SESSION variables. This would eliminate any requirement for hidden fields -- thereby reducing page transmission time (both ways).

pauladkins1974
11-18-2006, 03:59 AM
Hello So Is This (all)

I'm no PHP expert - but it I see this in action I'll be able to apply this to my site.

Does anyone have a link to an example of this or a tutorial?

Thanks

Paul

pauladkins1974
11-18-2006, 04:36 AM
Hello Again

I found an article on this and have got it working, being a real pain I have 3 questions about this.

1. I've just read that if a users browser doesn't accept cookies then session variables won't work, is there another solution that doesn't require cookies?

Or is this not generally considered a problem.

2. Assuming the above isn't an issue, I have tried going back in the multipage form I've created and changing the value of a field, the variable doesn't change, how can I allow my users to change a value if they want?

3. Also I use some select fields, is there anyway to have these default to the selected value once selected? It was easy with text boxes, I just echoed the variable but I don't know how to do this with selects, I need a way for my users to know that their data is saved.


Any help on these three is much appreciated.

Cheers

Paul

so_is_this
11-18-2006, 08:00 AM
1. PHP sessions can either use cookies (see session.use_only_cookies, and only one is required and only for the session id) or PHP will automatically attach the session id to each url in the page (see url_rewriter.tags and session.use_trans_sid).

2. If your page logic is coded correctly, then the user will be able to change anything you want them to change.

3. For this you just need something like this in the OPTION tag (the exact coding depends upon your exact needs):

<option value="<?php echo $opt_value; ?>" <?php if($sel_value == $opt_value) echo 'selected'; ?> ><?php echo $opt_text; ?></option>

pauladkins1974
11-19-2006, 11:25 AM
Thanks So Is This - I'll give it a try.

Cheers

Paul

pauladkins1974
11-20-2006, 04:52 AM
Hello So Is This (All)

Firstly thanks for all the help so far :) .

However I think my page logic must be wrong as once selected my variables seem to be set permanently.

I use the code below to set my variables:

session_register("sesstest");

$sesstest = $HTTP_POST_VARS['test'];


The code in my input fields:

<input type="text" name="test" value="<?php echo $_SESSION['sesstest]; ?>">

I have 3 pages in my multipart form, page one has the top piece of code for each variable in the form on that page, page 2 has the top piece of code for all the variables set on page 1 and 2. Finally page 3 defines the variables for all three pages at the top of the page.

When I click a link on page 3 taking me back to page 1.
Change the value in one of the fields progress to page 3 again and click a link back to page 1.
The variable I changed still shows the original value not the revised value.

Any ideas?

Cheers

Paul

so_is_this
11-20-2006, 10:12 AM
This is not the recommended approach for PHP sessions:

session_register("sesstest");

This does not allow for unique multiple simultaneous access to the same page within their own sessions. Better to just do this in each page which is to participate in the session:

session_start();

This will both generate a unique session id (if one does not already exist for that session) for each new visitor to the same page within the context of the current session and will register the generated session id to recover the current session as each page within the session is visited.

pauladkins1974
11-20-2006, 12:19 PM
Hello So Is This

I am using session_start(); above the rest of the code.

Are you saying that it isn't necessary to use session_register("sesstest"); as well?

My code currently looks like this:


session_start();

session_register("sesstest");

$sesstest = $HTTP_POST_VARS['test'];

Thanks again.

Cheers

Paul

carlh
11-20-2006, 12:31 PM
keep session_start() at the top of the page

to create a session variable just do $_SESSION['variablename']

then you can refer to $_SESSION['whatever'] until you use session_destroy()

from what i understand using http_post_vars is a deprecated method

pauladkins1974
11-20-2006, 12:37 PM
Hello Carlh

So skip session_register("sesstest"); then?

Cheers

Paul

carlh
11-20-2006, 12:49 PM
yep, if you want to write a small test script to make sure sessions are working it could look something like this:


<?PHP
session_start();

$var = 'oh please work';

$_SESSION['test'] = $var;

echo $_SESSION['test'];

session_destroy();
?>


and if you want the session to span across multiple pages just don't call session_destoy() until you're all done with your session variables

also in the php.ini file there's a line that lets you define where to save the session data to in case they don't have cookies enabled, I just have it set to C:\php\sessiondata

sb_
11-20-2006, 12:50 PM
Carl is right, session_register() is actually outdated and shouldn't be used... at least thats what I think it says.

pauladkins1974
11-21-2006, 08:27 AM
Thanks a lot guys - this worked a treat.

Cheers

Paul