Jump to content

Maintaining drop-down selection through multiple forms


modigy

Recommended Posts

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.