austin350s10 Posted May 29, 2009 Share Posted May 29, 2009 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- Quote Link to comment https://forums.phpfreaks.com/topic/160165-solved-simple-session-qi-think/ Share on other sites More sharing options...
JonnoTheDev Posted May 29, 2009 Share Posted May 29, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/160165-solved-simple-session-qi-think/#findComment-845035 Share on other sites More sharing options...
austin350s10 Posted May 29, 2009 Author Share Posted May 29, 2009 Thanks that works perfect!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/160165-solved-simple-session-qi-think/#findComment-845043 Share on other sites More sharing options...
JonnoTheDev Posted May 29, 2009 Share Posted May 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/160165-solved-simple-session-qi-think/#findComment-845048 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 after cleaning the values Not necessary in this case. You're just doing a comparison of two strings. Quote Link to comment https://forums.phpfreaks.com/topic/160165-solved-simple-session-qi-think/#findComment-845050 Share on other sites More sharing options...
JonnoTheDev Posted May 29, 2009 Share Posted May 29, 2009 Not necessary in this case. You're just doing a comparison of two strings. In this quick example, yes But you wouldn't want tainted data being placed back into the form values i.e. HTML tags, script injection, etc Quote Link to comment https://forums.phpfreaks.com/topic/160165-solved-simple-session-qi-think/#findComment-845054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.