checkbox to enable / disable hidden fields?

J

John

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.
 
J

John

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.

(e-mail address removed) (Danny) wrote in
 
D

David Dorward

John said:
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).

I'd do this on the server:

Pseudo code:

if (defined $album_to_search) {
push @songs_to_search, get_list_of_song_ids_from_albumid($album_to_search);
}

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

ASM

John said:
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.

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

or to get true / false as value of SearchAdd_1 :

<input type=checkbox onclick="SearchAdd_1.value= this.checked;">
 
D

David Dorward

ASM said:
<input type=checkbox onclick="SearchAdd_1.value= this.checked? 1 : 0;">

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.form.elements['SearchAdd_1'].value ...

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

ASM

John said:
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.

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

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

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


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

<form name="songsForm".... blah ....>
<p>Pre-selection :
all <input type=radio name="presel" onclick="wichSongs()">
some <input type=radio name="presel" onclick="wichSongs()">
any <input type=radio name="presel" onclick="wichSongs()">
<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.songsForm;
var r = f.presel;
var j = 0;
for(var i=0;i<r.length;i++) if(r.checked) j=i;
var p = f.getElementsByTagName('P');
for(var i=1;i<p.length;i++) {
if(p.className=='sg') {
p.getElementsByTagName('INPUT')[0].checked=false;
p.getElementsByTagName('INPUT')[0].value=0;
if(j==2) p.style.display='none';
else p.style.display='';
if(j==0) {
p.getElementsByTagName('INPUT')[0].checked=true;
p.getElementsByTagName('INPUT')[0].value=1;
}
}
}
}
 
A

ASM

David said:
ASM wrote:

[...]

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

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.
 
R

Richard Cornford

ASM said:
David Dorward wrote:
<input type="hidden" name="SearchAdd_1" value="0">
[...]

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

Yeap, but here we speak about JS
and I understood question was about JS :)
<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top