Forum Moderators: coopster
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.
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...
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.
...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.
<?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.
<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 }
...
?>