homepage Welcome to WebmasterWorld Guest from 184.72.69.79
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
php sessions for forms
How to do I update a variable by re-submitting a form?
raingerpete




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

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.

 

lorax




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

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...]

Slade




msg:1267641
 4:40 pm on Oct 14, 2002 (gmt 0)

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...

lorax




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

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.

Slade




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

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.

lorax




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

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?

Slade




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

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.

raingerpete




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

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.

dingman




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

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

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

lorax




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

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!

raingerpete




msg:1267649
 1:57 pm on Oct 17, 2002 (gmt 0)

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 }
...
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved