Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php sessions for forms

How to do I update a variable by re-submitting a form?

   
2:45 pm on Oct 14, 2002 (gmt 0)

10+ Year Member



hi,

I am using php sessions with three form pages that point to one.

I have three php form pages (form1.php, form2.php, form3.php) that post to one [hold.php] page, this allows me to let the user do the the parts (pages) of the form in any order (which is important).

But if I submit [form2.php] to [hold.php] and then hyperlink to [form2.php] or 'post' [hold.php] back to [form2.php]. Once at [form2.php] I can select another anwser and submit again but this does not change the variables held and displayed in [hold.php].

how do I get hold.php to take on the new values, also how do I get the old answers to re-appear on form.

4:02 pm on Oct 14, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



raingerpete,
Using sessions is wonderful and occassionally maddening experience. First, be sure Global variables are enabled. Second, I'm pretty sure that once you set a session var it cannot be changed using variables from a form POST or GET. You'll need to explicitly set it. You may want to review the info here: [php.net...]
4:40 pm on Oct 14, 2002 (gmt 0)

10+ Year Member



Enabling globals is not necessary. Unless you're trying to run old code, it might even be considered bad form.

When your form is submitted, all the variables are dumped to you in the appropriate $_GET or $_POST array. (For a form submit, most likely POST.)

Do read the Session info at: [php.net...] (will redirect to your language).

Session variables are an array that you have access to while sessions are enabled. If you want to set a session var, it would look something like:


$_SESSION['user_age'] = $_POST['age'];

Remember that you still should test the users submission to make sure it's the type of variable you are expecting, and is within a valid range.

Always sanity test your user's form submissions.

edit: I miss vbulletin's [php] tag...

5:41 pm on Oct 14, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Slade,
That's interesting - looks like I need to do some more reading!

rp,
Another thought, are you using session_start() on each of the pages? Without it, you'll only get the form vars but not have access to any of the session vars. Session_start() will recall all of the session vars and make them available to your script.

12:35 am on Oct 15, 2002 (gmt 0)

10+ Year Member



Just noticed this:

...also how do I get the old answers to re-appear on form.

When you load the form, set each field value to default to a variable(that you can process). At the beginning of the script that displays the form, test for the existance of the session variable. If it exists, copy the _session[] variable over the default one.

If a session variable exists, it will populate the form, if not, you'll get your pre-defined value.

1:12 am on Oct 15, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



If it exists, copy the _session[] variable over the default one

You could also just set the field's value = $var or is this what you meant Slade?

1:29 am on Oct 15, 2002 (gmt 0)

10+ Year Member



Something like this...

<?php
$field_dob = ""; //Date of Birth, default as blank
if (isset($_SESSION['dob'])) $field_dob = $_SESSION['dob'];
?>
Enter Date of Birth:
<input type="text" name="DOB" value="<?php echo $field_dob;?>">

You could use the session var in that echo, but I don't like to reference vars that might not exist yet...

This is just how I would do it... I don't claim it perfect or the best way, just a way that works.

8:07 pm on Oct 15, 2002 (gmt 0)

10+ Year Member



Thanks for the help!

Re: Re-filling a form with old answers
But what if the form uses drop-down boxes, how do I get the form to show the prevous 'selected' option.

8:15 pm on Oct 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<option selected="" value"whatever">whatever</option>

You'll have to dynamically generate the form page to stick 'selected' into the appropriate options, of course.

8:22 pm on Oct 15, 2002 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



If your options list comes from a db query and you're really good then you'll be able to evaluate for the selected option and not repeat it in the list!
1:57 pm on Oct 17, 2002 (gmt 0)

10+ Year Member



So how do I get this to work for :

<select name="Q01">
<option <?php print $val_0?> >value="0">value zero</option>
<option <?php print $val_1?> value="1">value one</option>
<option <?php print $val_2?> value="2">value two</option>
<option <?php print $val_3?> value="3">value three</option>
</select>

Would it go something like this?
and how would I make it work for all my questions, maybe using a function? as all the questions have a name format Q43


<?php
$val_0 = "";
$val_1 = "";
...
if $_SESSION['Q01'] = 0;
{ $val_0 == selected }
elseif $_SESSION['Q01'] = 1;
{ $val_1 == selected }
...
?>