checkbox to enable / disable hidden fields?

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

    checkbox to enable / disable hidden fields?

    Hello.

    I have a search form for music albums which among other things I need to
    search all the song titles of the song. Normally in a search form I would
    have checkboxes the user can use to select whether or not to search a given
    field.

    In this case it is not practical to have 20 checkboxes (each album can have
    up to 20 song titles).

    What I would rather do is have a single checkbox labeled "search song
    titles" and have all the song title fields hidden and either enabled or
    disabled as part of the search depending on the checked state of that check
    box.

    The hidden fields in question look like this:

    <input type="hidden" name="SearchAdd _1" value="1">
    <input type="hidden" name="SearchAdd _2" value="1">
    etc
    etc
    all the way to
    <input type="hidden" name="SearchAdd _20" value="1">

    So if I simply put all those in the search form then it will search those
    fields. Great. But how would I give the user the option to toggle them
    off or on in one shot?

    Thanks.
  • Danny

    #2
    Re: checkbox to enable / disable hidden fields?


    If the fields are 'hidden', there isn't much point on disabling them,
    unless you wish them disabled for them not to send along, which disabled
    fields do, you can do quite a few on an event like in this case, onclick on
    a checkbox, but you need to be specific on what you want, but is rather
    quite simple to do using a For loop.


    Danny

    Comment

    • John

      #3
      Re: checkbox to enable / disable hidden fields?

      Sorry, should have been more specific. Yes, what I mean is I want them all
      to not get sent along in the form. So effectively I need them to change
      from:

      <input type="hidden" name="SearchAdd _1" value="1">

      to

      <input type="hidden" name="SearchAdd _1" value="0">

      If a checkbox is unchecked, and back to 1 if it is checked.

      dann90038@blueb ottle.com (Danny) wrote in
      <1126325189.df9 99db5e087b893cb bc22d75bbbf24e@ teranews>:
      [color=blue]
      >
      > If the fields are 'hidden', there isn't much point on disabling
      > them,
      >unless you wish them disabled for them not to send along, which disabled
      >fields do, you can do quite a few on an event like in this case, onclick
      >on a checkbox, but you need to be specific on what you want, but is
      >rather quite simple to do using a For loop.
      >
      >
      > Danny
      >[/color]

      Comment

      • David Dorward

        #4
        Re: checkbox to enable / disable hidden fields?

        John wrote:
        [color=blue]
        > I have a search form for music albums which among other things I need to
        > search all the song titles of the song. Normally in a search form I would
        > have checkboxes the user can use to select whether or not to search a
        > given field.
        >
        > In this case it is not practical to have 20 checkboxes (each album can
        > have up to 20 song titles).[/color]

        I'd do this on the server:

        Pseudo code:

        if (defined $album_to_searc h) {
        push @songs_to_searc h, get_list_of_son g_ids_from_albu mid($album_to_s earch);
        }

        Much more reliable, and it doesn't require you to shunt all the song names
        to the client each time.

        --
        David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
        Home is where the ~/.bashrc is

        Comment

        • ASM

          #5
          Re: checkbox to enable / disable hidden fields?

          John wrote:[color=blue]
          > Sorry, should have been more specific. Yes, what I mean is I want them all
          > to not get sent along in the form. So effectively I need them to change
          > from:
          >
          > <input type="hidden" name="SearchAdd _1" value="1">
          >
          > to
          >
          > <input type="hidden" name="SearchAdd _1" value="0">
          >
          > If a checkbox is unchecked, and back to 1 if it is checked.[/color]

          <input type=checkbox onclick="Search Add_1.value= this.checked? 1 : 0;">

          or to get true / false as value of SearchAdd_1 :

          <input type=checkbox onclick="Search Add_1.value= this.checked;">


          --
          Stephane Moriaux et son [moins] vieux Mac

          Comment

          • David Dorward

            #6
            Re: checkbox to enable / disable hidden fields?

            ASM wrote:
            [color=blue][color=green]
            >> <input type="hidden" name="SearchAdd _1" value="0">
            >>
            >> If a checkbox is unchecked, and back to 1 if it is checked.[/color]
            >
            > <input type=checkbox onclick="Search Add_1.value= this.checked? 1 : 0;">[/color]

            Most browsers don't automatically create, for each element with a name, a
            variable with that name as a reference to that element.

            onclick="this.f orm.elements['SearchAdd_1'].value ...

            I still wouldn't use JavaScript to solve this problem though.

            --
            David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
            Home is where the ~/.bashrc is

            Comment

            • ASM

              #7
              Re: checkbox to enable / disable hidden fields?

              John wrote:[color=blue]
              > Sorry, should have been more specific. Yes, what I mean is I want them all
              > to not get sent along in the form. So effectively I need them to change
              > from:
              >
              > <input type="hidden" name="SearchAdd _1" value="1">
              >
              > to
              >
              > <input type="hidden" name="SearchAdd _1" value="0">
              >
              > If a checkbox is unchecked, and back to 1 if it is checked.[/color]

              function checkSongs(myFo rm,val) {
              var f = myForm;
              val = val? 1 : 0 ;
              for(var i=0;i<f.legnth; i++)
              if(f[i].name.indexOf(' SearchAdd')==0) f[i].value = val;
              }

              Check or uncheck all :
              <input type=checkbox onclick="checkS ongs(this.form, this.checked)">

              Uncheck all :
              <input type=button onclick="checkS ongs(this.form, false)" value=GO>


              ----------== other way ==--------

              <form name="songsForm ".... blah ....>
              <p>Pre-selection :
              all <input type=radio name="presel" onclick="wichSo ngs()">
              some <input type=radio name="presel" onclick="wichSo ngs()">
              any <input type=radio name="presel" onclick="wichSo ngs()">
              <p class=sg><input type=checkbox name="SearchAdd _1" value="0"> Title 1
              <p class=sg><input type=checkbox name="SearchAdd _2" value="0"> Title 2
              <p class=sg><input type=checkbox name="SearchAdd _3" value="0"> Title 3
              ....
              <p class=sg><input type=checkbox name="SearchAdd _20" value="0"> Title 20

              function whichSongs() {
              var f = document.songsF orm;
              var r = f.presel;
              var j = 0;
              for(var i=0;i<r.length; i++) if(r[i].checked) j=i;
              var p = f.getElementsBy TagName('P');
              for(var i=1;i<p.length; i++) {
              if(p[i].className=='sg ') {
              p[i].getElementsByT agName('INPUT')[0].checked=false;
              p[i].getElementsByT agName('INPUT')[0].value=0;
              if(j==2) p[i].style.display= 'none';
              else p[i].style.display= '';
              if(j==0) {
              p[i].getElementsByT agName('INPUT')[0].checked=true;
              p[i].getElementsByT agName('INPUT')[0].value=1;
              }
              }
              }
              }

              --
              Stephane Moriaux et son [moins] vieux Mac

              Comment

              • ASM

                #8
                Re: checkbox to enable / disable hidden fields?

                David Dorward wrote:[color=blue]
                > ASM wrote:
                >
                >[color=green][color=darkred]
                >>><input type="hidden" name="SearchAdd _1" value="0">[/color][/color][/color]
                [...]
                [color=blue]
                > I still wouldn't use JavaScript to solve this problem though.[/color]

                Yeap, but here we speak about JS
                and I understood question was about JS :)

                anyway JS is only an help an can't be used to do server's job.

                --
                Stephane Moriaux et son [moins] vieux Mac

                Comment

                • Richard Cornford

                  #9
                  Re: checkbox to enable / disable hidden fields?

                  ASM wrote:[color=blue]
                  > David Dorward wrote:[/color]
                  <snip>[color=blue][color=green][color=darkred]
                  >>>><input type="hidden" name="SearchAdd _1" value="0">[/color][/color]
                  > [...]
                  >[color=green]
                  >> I still wouldn't use JavaScript to solve this problem though.[/color]
                  >
                  > Yeap, but here we speak about JS
                  > and I understood question was about JS :)[/color]
                  <snip>

                  We may speak about JS here, but one of the things we are allowed to say
                  is that JS in an inappropriate technology for some tasks. I cannot see
                  anyone with a good understanding of server-side scripting (and
                  particularly the handling of form input with it, but that is probably
                  the first aspect of server-side scripting that anyone would learn) even
                  considering this. You just observe the state of the checkbox when the
                  request arrives at the server and act accordingly.

                  Richard.


                  Comment

                  Working...