Jump to content

Recommended Posts

I am trying to store the values a user entered into a form on my website so if they refresh the page to work they do doesn't disappear. Below is the code I am using on the form to store the values.

 

<?php
session_start();
if (isset($_SESSION['email'])){
   $email_value = "value=\"".$_SESSION['email']."\" ";
}
else{
   $email_value = "";
}
?>

 

This works great but it doesn't work for select lists.  How do I get a session to store a select list option value?

 

Thanks,

 

-Austin-

Link to comment
https://forums.phpfreaks.com/topic/160165-solved-simple-session-qi-think/
Share on other sites

Exactly the same only in a select list you use the argument 'selected' within the option element i.e.

<select name="test">
<option value="1"<?php if($_SESSION['test'] == 1) print " selected"; ?>>1</option>
<option value="2"<?php if($_SESSION['test'] == 2) print " selected"; ?>>2</option>
</select>

To be honest I would have just used $_POST (after cleaning the values) and not $_SESSION for this task as the data is not persistent anywhere else except the page with the form.

 

i.e.

<select name="test">
<option value="1"<?php if($_POST['test'] == 1) print " selected"; ?>>1</option>
<option value="2"<?php if($_POST['test'] == 2) print " selected"; ?>>2</option>
</select>

If using sessions you will have to make sure you destroy all of the values otherwise if a user comes back to the form all of the values will still be pre-filled.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.