Populate form values based on previous same form fields

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rizyak

    #1

    Populate form values based on previous same form fields

    This message is cross posted in
    alt.comp.lang.p hp & comp.lang.javas cript

    I have a form for a user to input an establishment's hours and what time an
    event is taking place. After the user inputs their establishment's hours of
    operation I want the form elements lower in the form to adjust so that an
    event can only happen when the place is open.

    I have two fields for the hours:
    These are both select fields with values between 0-23
    store_open
    store_close

    Later in the form I have
    event_start
    event_stop

    These values need to be between whatever store_open and store_close are.

    I am stuck.

    Thank you.




  • Mick White

    #2
    Re: Populate form values based on previous same form fields

    Rizyak wrote[color=blue]
    >
    > I have two fields for the hours:
    > These are both select fields with values between 0-23
    > store_open
    > store_close
    >
    > Later in the form I have
    > event_start
    > event_stop
    >
    > These values need to be between whatever store_open and store_close are.
    >
    > I am stuck.
    >
    > Thank you.[/color]

    One approach

    Number.protoype .isBetween=func tion(a,b){
    return this>=Math.min( a,b) && this<=Math.max( a,b);
    }

    if(!( +event_start < +event_end &&
    +event_start.is Between(+store_ open,+store_clo se) &&
    +event_stop.isB etween(+store_o pen,+store_clos e))){
    alert ("NO")
    }
    Mick

    Comment

    • Robert

      #3
      Re: Populate form values based on previous same form fields

      "Rizyak" <ryanREMOVEME@l atitude47.comAN DMETOO> wrote in message news:<ca5jc7$ke 4$1@nntp1.u.was hington.edu>...
      [color=blue]
      > I have a form for a user to input an establishment's hours and what time an
      > event is taking place. After the user inputs their establishment's hours of
      > operation I want the form elements lower in the form to adjust so that an
      > event can only happen when the place is open.
      >[/color]

      Here is one approach to validating the event times:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Verify time fields</title>

      <script type="text/javascript">

      // ------------------------------------------
      function validateAll()
      {
      alert("in validateAll");

      var x = document.forms["myForm"];
      var inputOK;

      if( x.storeOpen.val ue == "" )
      {
      alert("Enter a store open time.");
      inputOK= false;
      }
      else if( x.storeClose.va lue == "" )
      {
      alert("Enter a store close time.");
      inputOK= false;
      }
      else if( x.eventStart.va lue == "" )
      {
      alert("Enter an event start time.");
      inputOK= false;
      }
      else if( x.eventEnd.valu e == "" )
      {
      alert("Enter an event stop time.");
      inputOK= false;
      }
      else
      {
      inputOK = checkBoth();
      }

      alert("from validateAll. inputOK = " + inputOK);
      return inputOK;

      }

      // ............... ...............
      function checkBoth()
      {
      alert("In checkBoth...");

      var x = document.forms["myForm"];

      var inputOK;

      // Convert the times in string format to numeric format
      // in minutes.
      var storeOpen = convertTime(x.s toreOpen.value) ;
      var storeClose = convertTime(x.s toreClose.value );
      var eventStart = convertTime(x.e ventStart.value );
      var eventEnd = convertTime(x.e ventEnd.value);

      alert(".storeOp en.value = " + x.storeOpen.val ue +
      " storeOpen = " + storeOpen +
      "\n .storeClose.val ue = " + x.storeClose.va lue +
      " storeClose = " + storeClose +
      "\n .eventStart.val ue = " + x.eventStart.va lue +
      " eventStart = " + eventStart +
      "\n .eventEnd.value = " + x.eventEnd.valu e +
      " eventEnd = " + eventEnd );

      if ( eventStart > eventEnd )
      {
      alert("Event start must be before " +
      "or equal to event end.");
      x.eventStart.fo cus();
      inputOK = false;
      }
      else if ( !isBetween(even tStart,storeOpe n,storeClose) )
      {
      alert("Event start must occur " +
      "when the store is open.");
      x.eventStart.fo cus();
      inputOK = false;
      }
      else if ( !isBetween(even tEnd,storeOpen, storeClose) )
      {
      alert("Event end must occur " +
      "when the store is open.");
      x.eventEnd.focu s();
      inputOK = false;
      }
      else
      {
      inputOK = true;
      }


      return inputOK;
      }

      // ............... ............... .......
      function convertTime(tim eString)
      {

      //Convert to a minute based value.
      //Input may either be in 24 hour format
      // or am/pm format.
      // Examples 8:00am, 8, 8:12, 14:30, 2:30pm, or 4:30P.M.

      var theTime = parseInt(timeSt ring,10) * 60;

      if ( timeString.inde xOf("p") >= 0 ||
      timeString.inde xOf("P") >= 0 )
      {
      theTime += 12*60;
      }

      var minutesIndex = timeString.inde xOf(":");
      if ( minutesIndex >= 0 )
      {
      var minutes = timeString.subs tr(minutesIndex +1);
      theTime += parseInt(minute s,10);
      }

      return theTime;
      }

      // ............... ............... ........
      function isBetween(test, a,b)
      {
      return test>=a && test<=b;
      }


      </script>
      </head>

      <body>

      <p>Please try out our form.</p>

      <form name="myForm"
      action="http://www.notavalidwe baddress.com"
      method="POST"
      onSubmit="retur n validateAll();" >

      <p>Store start time:<br>
      <input type="text" name="storeOpen " size="20"><br>< br>
      Store end time:<br>
      <input type="text" name="storeClos e" size="20"><br>
      </p>
      <p>The little event times.</p>
      <p>Event start time:<br>
      <input type="text" name="eventStar t" size="40">
      <p>Event end time<br>
      <input type="text" name="eventEnd" size="40">
      </p>

      <p><input type="submit" border="0" value="Submit">
      </form>

      </body>
      </html>

      Robert

      Comment

      • Lee

        #4
        Re: Populate form values based on previous same form fields

        Rizyak said:[color=blue]
        >
        >This message is cross posted in
        >alt.comp.lang. php & comp.lang.javas cript
        >
        >I have a form for a user to input an establishment's hours and what time an
        >event is taking place. After the user inputs their establishment's hours of
        >operation I want the form elements lower in the form to adjust so that an
        >event can only happen when the place is open.
        >
        >I have two fields for the hours:
        >These are both select fields with values between 0-23
        >store_open
        >store_close
        >
        >Later in the form I have
        >event_start
        >event_stop
        >
        >These values need to be between whatever store_open and store_close are.[/color]

        The two answers I've seen have been describing how to validate the
        input values to make sure they're in the correct range. Is that
        what you're asking about, or are you trying to change the choices
        that are available in Select menus?

        You might want to be more flexible than that, anyway.
        One of my favorite establishments lists their hours as
        11:30am to 2am, but periodically hosts an event that
        starts at 11am.

        Comment

        • Rizyak

          #5
          Re: Populate form values based on previous same form fields

          Indeed Lee is correct.
          I am trying to change choices.
          Envision selecting a state and a list of cities come up. I won't need to
          make a database call like this, but it is similar ideas. Parent child
          relationships.
          I have come up with a solution that uses refresh and then I ran into
          problems with my other form elements going away. Then I stored them as
          cookies and had focus issues on refresh then I decided there has got to be
          an easier way. If possible I would like to stick with dhtml....
          I think the flexibility that lee was talking about won't be a problem once
          the solution is found.
          Thanks!



          "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
          news:cad0r20oid @drn.newsguy.co m...[color=blue]
          > Rizyak said:[color=green]
          > >
          > >This message is cross posted in
          > >alt.comp.lang. php & comp.lang.javas cript
          > >
          > >I have a form for a user to input an establishment's hours and what time[/color][/color]
          an[color=blue][color=green]
          > >event is taking place. After the user inputs their establishment's hours[/color][/color]
          of[color=blue][color=green]
          > >operation I want the form elements lower in the form to adjust so that an
          > >event can only happen when the place is open.
          > >
          > >I have two fields for the hours:
          > >These are both select fields with values between 0-23
          > >store_open
          > >store_close
          > >
          > >Later in the form I have
          > >event_start
          > >event_stop
          > >
          > >These values need to be between whatever store_open and store_close are.[/color]
          >
          > The two answers I've seen have been describing how to validate the
          > input values to make sure they're in the correct range. Is that
          > what you're asking about, or are you trying to change the choices
          > that are available in Select menus?
          >
          > You might want to be more flexible than that, anyway.
          > One of my favorite establishments lists their hours as
          > 11:30am to 2am, but periodically hosts an event that
          > starts at 11am.
          >[/color]


          Comment

          Working...