Jump to content

[SOLVED] simple session Q...i think


austin350s10

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.

Archived

This topic is now archived and is closed to further replies.

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