Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1049
  • Last Modified:

Posting disabled checkbox value in ASP

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
geoenvi
Asked:
geoenvi
2 Solutions
 
ZvonkoSystems architectCommented:
You can enable the checkbox on form submit time.
Like this:
<form action="some.asp" onSubmit="this.chkboxB.readonly=false;">


0
 
dorwardCommented:
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
 
lil_puffballCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZvonkoSystems architectCommented:
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
 
GwynforWebCommented:
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
 
lil_puffballCommented:
Hmmm...Zvonko that's a good idea. :)
0
 
lil_puffballCommented:
Wow...4 posts in the same minute...that must be a rare occurence. ;)
0
 
ZvonkoSystems architectCommented:
I think hidden field is the better idea.
0
 
ZvonkoSystems architectCommented:
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
 
devicCommented:
>>>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
 
geoenviAuthor Commented:
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
 
lil_puffballCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now