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
https://forums.phpfreaks.com/topic/260777-save-undefined-value-from-checkbox/
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.

 

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.