0

I have a form with checkboxes:

<input class="checkbox" type="checkbox" name="exc[57]" value="57" checked>
<input class="checkbox" type="checkbox" name="exc[51]" value="51" checked>

When I uncheck a checkbox is getting posted while it should not according to http://www.w3schools.com/jsref/prop_checkbox_value.asp

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Spyros
  • 313
  • 4
  • 12
  • Did you try different browsers? In my up-to-date FF only checked fields are posted. Please give us more information (complete minimal example page, post target etc.) – Daniel Calliess Nov 07 '12 at 14:09
  • I have tried here: [link](http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_checkbox_value) - OS: Windows7 x64. - Chrome 22.0.1229.94: Un/Checked get Bike - Firefox 16.0.2: Un/Checked get Bike - IE 9: Un/Checked get Bike - Opera 11.61: Un/Checked get Bike - Safari 5.1.7: Un/Checked get Bike - Problem on all browsers. – Spyros Nov 08 '12 at 16:26
  • _Posting_ normally means sending data to the server which is not exactly what you're doing in your code example! – Daniel Calliess Nov 10 '12 at 11:28

1 Answers1

0

If you submit the form to the server, the value won't be transfered in the post data. But if you access the field's value property from client script, you will get the actual data inside the value attribute. If you want to know if the box has been checked, use the checked property instead:

<!DOCTYPE html>
<html>
<head>
    <script>
    function displayResult()
    {
        var x=document.getElementById("bike").checked;
        alert(x);
    }
    </script>
</head>
<body>
    <form>
        <input type="checkbox" id="bike" value="Bike"> I have a bike<br>
    </form>
    <button type="button" onclick="displayResult()">Display value</button>
</body>

Daniel Calliess
  • 861
  • 6
  • 12
  • in my code I have: `` I uncheck the box. When I post I get with print_r `Array ( [51] => 51 [50] => 50 ... [57] => 57 )` 57 should not be included since it was unchecked. – Spyros Nov 10 '12 at 14:27
  • Could you have a look at the actual data sent by the browser using your favorite browser developer tools or a tool like [Fiddler](http://www.fiddler2.com/fiddler2/)? And please update your code example so we can see what exactly is going on. – Daniel Calliess Nov 10 '12 at 14:38