Jump to content

save undefined value from checkbox


newphpcoder

Recommended Posts

Hi..

 

I have checkbox and I need to get his value.

 

I create function for checkbox if checkbox is clicked it is true then the checkbox value is H else W.

 

function chk(){

if(document.getElementById('H').checked==true){
var H = document.getElementById('H'). value = H;
}

else if(document.getElementById('H').checked==false){
var H = document.getElementById('H'). value = W;
}

}

 

and I have function for save :

 

function ApproveLeaveOP(){
    var H = document.getElementById('H').value;
    document.sampleform.action="AddLeave.php?H="+H;
    document.sampleform.submit();  

}
<input type="checkbox" name="H" id="H" value="" onclick="chk()">   

 

I don't know how can I add value to the checkbox if I click the value will be H if not the value is W.

 

I am new in using checkbox.

Thank you so much..

Link to comment
Share on other sites

If you need two mutually exclusive values you should be using two radio buttons or a select element, not a checkbox.  Checkboxes have one value which is either active or not depending on the checked state.  Using JS to change it like your attempting to do is a horribly wrong usage or checkboxes.

 

Link to comment
Share on other sites

<input type="radio" name="approveLeave" value="H">
<input type="radio" name="approveLeave" value="W">

 

That will make two radio buttons on the page, and the user will only be able to select one or the other.  When you submit them in your server-side script they will be under the field name approveLeave with a value of either H (if the first is picked) or W (if the second).

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.