Welcome Guest, Not a member yet? Register   Sign In
Form validation and SELECTs
#1

[eluser]jppi_Stu[/eluser]
(Solved: Disregard!)

I must be missing something obvious. When I submit a form that fails validation and the form is redisplayed, how is the browser ignoring the SELECTED attribute in an OPTION of a SELECT menu?

Here's an example... Let's say I have a form that includes the following:
Code:
<?php
    # Decode the cheese field
    if ($this_food->cheese == 'Cheddar') {
       $dbcheddar = TRUE;
       $dbwensleydale = FALSE;
   }
   else {
       $dbcheddar = FALSE;
       $dbwensleydale = TRUE;
   }
?>
<select id="cheese" name="cheese">
  <option value="">Choose your cheese</option>
  <option value="Cheddar" &lt;?php echo(set_select('cheese','Cheddar',$dbcheddar)); ?&gt;>Cheddar</option>
  <option value="Wensleydale" &lt;?php echo(set_select('cheese','Wensleydale',$dbwensleydale)); ?&gt;>Wensleydale</option>
</select>
When the form is first shown, the data is preloaded from a database, and the generated code might look like this:
Code:
<select id="cheese" name="cheese">
  <option value="">Choose your cheese</option>
  <option value="Cheddar" selected="selected">Cheddar</option>
  <option value="Wensleydale">Wensleydale</option>
</select>
Cheddar will be preselected for me, per the value in the database. I then cleverly change my cheese selection to Wensleydale. However, I not-so-cleverly neglect some other part of the form, causing $this->form_validation->run() to return False. The form will redisplay, and (via validation_errors()), I get a message indicating where I went wrong. The important part is that Wensleydale will remain selected -- yet examining the HTML code for the redisplayed form shows the SELECTED attribute still on the Cheddar option! Clearly this does not seem correct, and not just because I dislike cheddar cheese. Tastes in cheese aside, I wouldn't normally care as long as everything works out in the end, but I have a SELECT menu with a nasty habit of not exhibiting this behavior, no matter how I try to retain the user's choice. That's what made me look at the generated HTML code to begin with.

(As an aside, I have to wonder about the selected="selected" result of set_select() given that selected is a boolean attribute.)

So... what am I missing?


Messages In This Thread
Form validation and SELECTs - by El Forum - 10-19-2010, 02:05 AM
Form validation and SELECTs - by El Forum - 10-19-2010, 03:52 AM
Form validation and SELECTs - by El Forum - 10-19-2010, 03:17 PM
Form validation and SELECTs - by El Forum - 10-19-2010, 03:48 PM
Form validation and SELECTs - by El Forum - 10-19-2010, 04:01 PM
Form validation and SELECTs - by El Forum - 10-19-2010, 07:43 PM
Form validation and SELECTs - by El Forum - 10-19-2010, 07:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB