doubledee Posted March 27, 2012 Share Posted March 27, 2012 Okay, really newbie question, but for this code... <!-- Gender --> <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value="">--</option> <option value="F">Female</option> <option value="M">Male</option> </select> 1.) How do I assign a variable to this? 2.) How do I make this "sticky"? Here is how I have usually done other form types... <!-- First Name --> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" type="text" maxlength="30" value="<?php if(isset($firstName)){echo htmlentities($firstName, ENT_QUOTES);} ?>" /><!-- Sticky Field --> <?php if (!empty($errors['firstName'])){ echo '<span class="error">' . $errors['firstName'] . '</span>'; } ?> Oh, by the way, at the top of my PHP file I have this code... if ($_SERVER['REQUEST_METHOD']=='POST'){ // Form was Submitted (Post). // Initialize Errors Array. $errors = array(); // Trim all form data. $trimmed = array_map('trim', $_POST); Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259791-select-list-assigning-variable-and-making-sticky/ Share on other sites More sharing options...
trq Posted March 27, 2012 Share Posted March 27, 2012 <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value="">--</option> <option value="F"<?php (isset($gender) && $gender == "F") ? 'selected="selected"' : ''; ?>>Female</option> <option value="M"<?php (isset($gender) && $gender == "M") ? 'selected="selected"' : ''; ?>>Male</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/259791-select-list-assigning-variable-and-making-sticky/#findComment-1331527 Share on other sites More sharing options...
scootstah Posted March 27, 2012 Share Posted March 27, 2012 1. The same way as any other form element. $_POST['gender'] 2. thorpe beat me to it. EDIT: Although, he forgot echo's. <label for="gender">Gender:</label> <select id="gender" name="gender"> <option value="">--</option> <option value="F"<?php echo (isset($gender) && $gender == "F") ? 'selected="selected"' : ''; ?>>Female</option> <option value="M"<?php echo (isset($gender) && $gender == "M") ? 'selected="selected"' : ''; ?>>Male</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/259791-select-list-assigning-variable-and-making-sticky/#findComment-1331530 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.