modigy Posted July 17, 2006 Share Posted July 17, 2006 Hi all,I've got a form that the client uses to input books. Currently I've been using sessions to maintain the text typed in some of the text fields. Sometimes my client has to enter many books with some of the same information. For example the client enters several books from the same author. I use the session to maintain that variable so they only have to enter the author once, click submit and then the client is given the same form to enter then next book 'without' having to type the author's name again. I have been searching and trying to use my session variables to maintain a dropdown selection as well. I have several dropdowns, but as an example: If the client wants to select the type of book (drama, scifi, horror, etc.) and they have many books to enter, they can just select 'drama', click submit and all consequtive forms will already have drama selected. I can't seem to get this to work. Any and all help would be appreciated:[b]addbook.php[/b][code]<tr><td><span class="formcontent">Theme:</span></td><td><select name="theme" id="theme"><option value="">Choose Theme</option><option value="drama">Drama</option><option value="fiction">Fiction</option><option value="Inspirational">Inspirational</option><option value="Mystery">Mystery</option><option value="Sports">Sports</option><option value="thriller">Thriller</option></select><?if (isset ($_SESSION['theme'])) {echo '<option value="'.$_SESSION['theme'].'" selected="selected">'.$_SESSION['theme'].'</option>';echo '<br>the session variable is NOT empty';} else {echo '<br>the session variable is empty';}?> </td></tr>[/code][b]Please note[/b] the session is already working for the textfields. Here is a small portion of what is listed on submitbook.php (I have not included my text field session variables and calls).[b]submitbook.php[/b][code]$_SESSION["theme"] = "$theme";header('Location: addbook.php');[/code]Thanks for your help in advance :)M Quote Link to comment https://forums.phpfreaks.com/topic/14894-maintaining-drop-down-selection-through-multiple-forms/ Share on other sites More sharing options...
hitman6003 Posted July 18, 2006 Share Posted July 18, 2006 I think this may be what you are trying to do:[code]<?php//code to process your form ....//at the end of your form processing//you should end up with your vars from //the previous posting...$prevtheme = $_POST['theme'];$prevauthor = $_POST['author'];//generate the select box$themes = array( 'drama' => 'Drama', 'fiction' => 'Fiction', 'Inspirational' => 'Inspirational' //etc...);$themeselect = ' <select name="theme" id="theme"> <option value="">Choose Theme</option>';foreach ($themes as $key => $value) { $themeselect .= '<option value="' .$key . '"'; if ($prevtheme == $key) { $themeselect .= " selected"; } $themeselect .= '>' . $value . '</option>' . "\n";}$themeselect .= "</select>";?><!-- html for your form goes here --><input type="text" name="author" value="<?php echo $prevautor; ?>" size="25" /><br /><?php echo $themeselect; ?><!-- rest of form... -->[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14894-maintaining-drop-down-selection-through-multiple-forms/#findComment-59702 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.