Jump to content


Photo

Maintaining drop-down selection through multiple forms


  • Please log in to reply
1 reply to this topic

#1 modigy

modigy
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationMadrid, Spain

Posted 17 July 2006 - 10:54 PM

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:

addbook.php

<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>

Please note 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).

submitbook.php

$_SESSION["theme"] = "$theme";
header('Location: addbook.php');

Thanks for your help in advance  :)

M
Code now, you'll get all the sleep you need when your Dead!!

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 18 July 2006 - 12:37 AM

I think this may be what you are trying to do:

<?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... -->





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users