Solved

Posting disabled checkbox value in ASP

Posted on 2004-03-25
12
1,041 Views
Last Modified: 2012-08-13
I have two checkboxes chkboxA and chkboxB.
If someone clicks on chkboxA, I need to check the chkboxB and disable it so that chkboxB can be unchecked only by unchecking the chkboxA.  If chkboxA is unchecked then the users are free to check/uncheck the chkboxB. (Code below that works fine to do this.

However, when I try to submit the form in ASP, the disabled chkbox value does not get posted.  I have since understood that ASP does not post disabled values during submit.  What are my options here? Any other way to make the chkbox readonly/disabled from Javascript?




I used the following code that works fine to do this :
******************************************************************************************
function disablechkboxB()
{
    if (document.NewuserForm.chkboxA.checked==true)
    {
   
    document.getElementById('td1').style.color='Gray';
    document.NewuserForm.chkboxB.checked=true;
   document.NewuserForm.chkboxB.readonly=true;
    }
    else
    {
     document.getElementById('td1').style.color='Black';
    document.NewuserForm.chkBoxB.checked=false;
    document.NewuserForm.chkBoxB.disabled=false;
   
    }
    return true;
}
*******************************************************************************************
0
Comment
Question by:geoenvi
12 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 350 total points
ID: 10682333
You can enable the checkbox on form submit time.
Like this:
<form action="some.asp" onSubmit="this.chkboxB.readonly=false;">


0
 
LVL 17

Expert Comment

by:dorward
ID: 10682342
Browsers don't post disabled form controls in HTML documents - the ASP is irrelevent.

Use readonly instead of disabled. (Disabled really does mean disabled)
0
 
LVL 12

Assisted Solution

by:lil_puffball
lil_puffball earned 150 total points
ID: 10682343
You can use a hidden field to do this. When getting the checkbox value, do it like this:

chkboxB=Request.form("chkboxBValue")

instead of

chkboxB=Request.form("chkboxB")

Here is an example:

<script>
function disablechkboxB()
{
    if (document.NewuserForm.chkboxA.checked==true)
    {
    document.getElementById('td1').style.color='Gray';
    document.NewuserForm.chkboxB.checked=true;
   document.NewuserForm.chkboxB.disabled=true;
   document.NewuserForm.chkboxBValue=true;
    }
    else
    {
     document.getElementById('td1').style.color='Black';
    document.NewuserForm.chkboxB.checked=false;
    document.NewuserForm.chkboxB.disabled=false;
   document.NewuserForm.chkboxBValue=false;    
    }
    return true;
}
</script>

<form name=NewuserForm>
Checkbox A: <input type=checkbox name=chkboxA onclick="disablechkboxB();"><br>
Checkbox B: <input type=checkbox name=chkboxB onclick="this.form.chkboxBValue.value=this.checked;"><br>
<input type=hidden name=chkboxBValue>
</form>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10682347
Of course you can put that single statement also inside your form validation function if onLoad is already used by that validation function call.
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 10682348
Enable all the checkboxes on submission, eg

<script>
function enable(form){
form.chkBoxA.disabled=false;
form.chkBoxB.disabled=false;
return true
}
</script>

<form name=f onsubmit='enable(this)'>
<input type=checkbox name=chkboxA>
<input type=checkbox name=chkboxB>
</form>
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10682356
Hmmm...Zvonko that's a good idea. :)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 12

Expert Comment

by:lil_puffball
ID: 10682366
Wow...4 posts in the same minute...that must be a rare occurence. ;)
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10682373
I think hidden field is the better idea.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10682420
Hello geoenvi,

in your profile I saw that you regulary give grading B and C
Don't you know that grading A does not cost you more :)
And by the way, we experts get more expert points when you grade A.
So please be so kind :-)
0
 
LVL 25

Expert Comment

by:devic
ID: 10682674
>>>However, when I try to submit the form in ASP, the disabled chkbox value does not get posted.
even is not disabled and not checked value does not get posted.

>>>Code below that works fine to do this

here some modification ;)




<script>
function disablechkbox(obj,f)
{
      obj.parentNode.style.color=obj.checked?"gray":"black";
      obj.form.elements[f].disabled=obj.checked;
      return true;
}
</script>
<table cellspacing=0 cellpadding=0 border=1>
      <form  action=http://www.google.com/search/? name=NewuserForm >
<tr>      <td>
            Select Type:<br>
            A<input type=checkbox value=A name=chkboxA onclick=disablechkbox(this,"chkboxB")>
            B<input type=checkbox value=B name=chkboxB onclick=disablechkbox(this,"chkboxA")>
      </td>
</tr>
<tr>
      <td><input type=submit></td>
</tr>      
      </form>
</table>
0
 

Author Comment

by:geoenvi
ID: 10684008
puffball,
Tried your method before and also tried it now.  Here is what is happening. The click event for ChkBoxB where you are assigning the value to a hidden variable is not being fired if the check is placed from Javascript (function disablechkboxB())

Since the event is not being fired, the value is not being assigned to the hidden field.

Zvonk,
Your suggestion does look appealing.  I will try that in the next few mins.  Something tells me that I can set the disabled (not readonly) property back to true during form submit, I should be good to go.  If it works, I have no problems giving you an A.  Thanks for all your help.  I will get back in a few....


0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10684041
Oops, sorry, I notice I made a mistake in the function. If you are still interested in doing it my way, here is the corrected function:

function disablechkboxB()
{
    if (document.NewuserForm.chkboxA.checked==true)
    {
    document.getElementById('td1').style.color='Gray';
    document.NewuserForm.chkboxB.checked=true;
   document.NewuserForm.chkboxB.disabled=true;
   document.NewuserForm.chkboxBValue.value=true;
    }
    else
    {
     document.getElementById('td1').style.color='Black';
    document.NewuserForm.chkboxB.checked=false;
    document.NewuserForm.chkboxB.disabled=false;
   document.NewuserForm.chkboxBValue.value=false;    
    }
    return true;
}

You may also want to set the value of the hidden field to false as a default.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now