Solved

Posting disabled checkbox value in ASP

Posted on 2004-03-25
12
1,042 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now