Form validation and SELECTs |
[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 Code: <select id="cheese" name="cheese"> (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?
[eluser]Watermark Studios[/eluser]
It seems to me that your object $this_food is not retaining the changes due to the validation error. I would create a trigger that maintains the state of the selected option despite validation. In other words, create another variable that holds the value you want it to hold instead of asking the invalid object to hold that value. As a note, you can use SELECTED, SELECTED = "TRUE", SELECTED = "SELECTED. Any one of those attributes will have the same results. If I remember correctly, assigning anything to SELECTED makes it valid. "When set, this boolean attribute specifies that this option is pre-selected." - from www.w3.org Thanks, Ken
[eluser]jppi_Stu[/eluser]
Thanks for the input. What is most confusing to me is how the browser is displaying something that is not supported by the underlying source HTML, as far as pre-selecting an OPTION in the SELECT menu. (However, the validation failure message is visible in the source and the output, so clearly the browser is heeding the source to that extent.) In fact, if I do a "view source" and copy the whole thing to a new file, and open that new .html file in a browser, it shows what I would expect -- in the example case, Cheddar would be pre-selected because that is what has the SELECTED attribute set. It's only within the context of CodeIgniter redisplaying the form that the underlying source does not match the browser output (and I've checked multiple browsers to see if it was possibly browser-specific). I'm not too worried about getting the right result in the underlying HTML, as I'm sure I can make that happen, whether or not it's in a CI-friendly way or not. But to have the browser not pre-select the OPTION that has SELECTED set is confounding me. FWIW, regarding my cheesy example, the controller is loading $this_food like this: Code: function grocery_prefs()
[eluser]danmontgomery[/eluser]
selected="selected" is the correct XHTML standard syntax... the same with checked="checked", disabled="disabled", etc. http://www.w3schools.com/Xhtml/xhtml_syntax.asp What you're saying doesn't make sense... The browser is completely ignorant of whether you're running CI or not. What you're seeing is probably a browser cached page, or a function of some browser addon.
[eluser]jppi_Stu[/eluser]
[quote author="noctrum" date="1287542925"]What you're saying doesn't make sense... The browser is completely ignorant of whether you're running CI or not. What you're seeing is probably a browser cached page, or a function of some browser addon.[/quote] I agree it doesn't make sense -- which is why I'm puzzled enough to ask here about it. :cheese: The page isn't cached since the validation error message is present. And it's not a browser add-on, as I've tested it in multiple browsers, including browsers I wouldn't normally use and thus wouldn't customize (not that I'm the type to use add-ons anyway, actually). (Oh, and thanks for the XHTML info. See what happens when you get away from serious Web development for a few years?)
[eluser]jppi_Stu[/eluser]
OK! :red: When things don't make sense, it means you're missing something. Right? So, now things make sense again, because I found what I was missing... I thought I was being diligent in checking this scenario in multiple browsers, but that's where I missed something. This strange scenario ended up being limited to one browser -- Google Chrome in OpenSUSE 11.3 x86_64, to be specific -- but I didn't catch what I'd missed until this evening. Viewing the source in Chrome actually was viewing the (cached?) pre-submit/validation version of the page. The validation failure message was appearing in the source for all the browsers except Chrome. Too hasty, too sleepy, whatever... Just glad I found the quirk that was throwing me off. Why I was using Chrome as my development browser is another matter entirely. I think I'll go back to Opera or Firefox. :roll:
[eluser]SitesByJoe[/eluser]
Firefox on Mac has "sticky" select menus too. If you just hit refresh while testing forms it'll deceive you and ignore your selected="selected". This issue has driven me nuts working on forms many times. |
Welcome Guest, Not a member yet? Register Sign In |