0

I have an exam and what I am looking to do use session to save every value, whether it's textboxes or selects or radio button groups choices. So if the user submits the form and misses something, coming back to the form from a different page, it keeps all the value that was entered and auto fills it in. I would also like to keep the session alive for 2 hours. I know I can use something of the following:

FORM PAGE:

<form method=post action="submit.php">
    <input type=text value="" name="first" />

    <input type=radio value="A" name="acct" /> A
    <input type=radio value="B" name="acct" /> B

    <input type=radio value="A" name="birt" /> A
    <input type=radio value="B" name="birt" /> B
</form>

On submission of the form in the submit.php page I have this:

    <?php
    session_start();
    $_SESSION['textbox1'] = isset($_POST[first]) ? $_POST[first] : null;
    $_SESSION['radiogroup1'] = isset($_POST[acct]) ? $_POST[acct] : null;
    $_SESSION['radiogroup2'] = isset($_POST[birt]) ? $_POST[birt] : null;

** PLUS OTHER CODES I USE **
    ?>

The above should start a session and save those values so no matter if I exit the page or not, it should store it. Correct?

Now questions I have are:

  • How do I make the session alive for ONLY 2 hours
  • Let's say I exit the page or go to another page and I come back to the FORM page, how do I auto enter the value which was originally saved in the session?
  • In WHAT SHOULD I ENTER HERE How do I accomplish, if the user selected a value fill it in otherwise leave it alone? SOLVED

I really appreciate the help.

Si8
  • 9,141
  • 22
  • 109
  • 221
  • 1
    Put `null` instead of *WHAT SHOULD I ENTER HERE* – jraede Jun 13 '13 at 19:19
  • Thank you. Let me edit it right away. – Si8 Jun 13 '13 at 19:20
  • 1
    As you know, and if you don't.. when a user exits the browser, all the sessions are destroyed automaticly. for asking about how to make the session alive for only 2 hours, heres an answer i've found:[session timeout](http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes) – Orel Biton Jun 13 '13 at 19:21
  • Not true at all - the session ID is stored in a cookie, and you have control over how long that cookie lasts and when/if it is destroyed. It CAN be destroyed when they exit the browser, but doesn't at all have to be. – jraede Jun 13 '13 at 19:22
  • I agree @OrelBiton in exiting the web browser. And where would I add the `session_destroy();` in the code above or to check for it? – Si8 Jun 13 '13 at 19:23

3 Answers3

2

1st answer:

SESSIONS remain alive until the user closes his/her browser so you need to make a cookie like this:

setcookie("TestCookie", $value, time()+(3600*2));  //save for 2 hours

2nd answer:

Use the array : $_COOKIE['session_name']

3rd answer:

Just use null there.

Simple :)

Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
1

To add the submitted data to the form, jut output the session to the value of all the inputs:

  <input type=text value="<?php echo  $_SESSION['textbox1'];?>" name="first" />
Skarlinski
  • 2,419
  • 11
  • 28
1

To autofill do something like:

<input type=radio value="A" name="acct" <?php echo ($_SESSION['acct'] == A) ? "checked" : "" ?>/>
immulatin
  • 2,118
  • 1
  • 12
  • 13