Form Select Default

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Form Select Default

    I have a form with three fields for date of birth, day/month/year. When a user selects their dob, if not all details on the form are correct then the form is reloaded with any data entered already still there for the user, the problem i have is keeping the dob selected in the select fields, say a user select 21/04/1988 the three dob selects would default to dd/mm/yyyy which are the three default values, i need it to stay on the user selected date the user entered!??
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Set the options as selected, e.g.
    Code:
    <option value="24" selected="selected">24</option>

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Of course, HTML won't be able to tell you which option was previously selected; you'll need javascript (or something else) for that.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        I assumed that form reload meant a page reload, so you'd use server-side code to determine which option should be selected.

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Can I suggest using javascript to validate the form before it is submitted to the server to process. Not only does this save on server load, but it also is a quicker response for the user.
          Basically you write some javascript code which checks that all the values entered are valid before it sends it to be processed. This is not 100% because a small percentage of users have javascript disabled, but it should work for most of your visitors... The worth while ones atleast :P
          Google "Validate forms javascript" or something like that to get some sample codes.

          Comment

          • ziycon
            Contributor
            • Sep 2008
            • 384

            #6
            Originally posted by TheServant
            Can I suggest using javascript to validate the form before it is submitted to the server to process. Not only does this save on server load, but it also is a quicker response for the user.
            Basically you write some javascript code which checks that all the values entered are valid before it sends it to be processed. This is not 100% because a small percentage of users have javascript disabled, but it should work for most of your visitors... The worth while ones atleast :P
            Google "Validate forms javascript" or something like that to get some sample codes.
            I've done all the validation through the PHP script as not everyone will have javascript enabled!

            Go the 'selected' thing working, thanks!

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              It is a good idea to have both javascript validation as well as PHP validation. It reduces the number of requests and saves the user time as I mentioned. Anyway, glad to hear it's working.

              Comment

              Working...