Reputation: 83
I'm trying to change the value of my check-box, I want the value of the check-box to equal 'N', when the check-box is not selected. And then the value of the same check-box changed to 'Y' if the check-box is selected. This value will then be saved to my database. At the moment, when I save the check-box, the value is saved as 'ON' when selected and my database field remains blank if the check-box is not selected when I save to my database. Please see code below.
<script language="javascript">
function validate() {
if (document.getElementById('box1').checked) {
alert("checked");
document.getElementById('box1').value == 'Y'
} else {
alert("Checkbox not checked!")
document.getElementById('box1').value == 'N'
}
}
</script>
........................
<td><input type="checkbox" name="chkbox3" id="box1" onclick="validate()"> Letter</td>
Any help would be much appreciated.Thanks.
Upvotes: 0
Views: 6468
Reputation: 2450
Basicly, I think this is a problem can be solved by "post form".
When you click the element checkbox, you trigger a function "validate()". Then it change the value of checkbox.
When this input element is wrapped by a form like this:
<form method="post" action="/your/url/to/handle/data" name="postForm">
<input type="checkbox" name="chkbox3" id="box1" onclick="validate()"> Letter
</form>
Your can add a "input submit" element to trigger this form to post:
<form method="post" action="/your/url/to/handle/data" name="postForm">
<input type="checkbox" name="chkbox3" id="box1" onclick="validate()"> Letter
<input type="submit" value="submit form" /> <!-- here is the trigger input -->
</form>
Then in your handle page,you can receive the key-value in this form.
P.S the checkbox'key in form element is "name" not "id". In this demo, you received the data like: {chkbox3:'Y'}
That's all, hope it can solve your problem. : )
Upvotes: 2