JavaScript How do I reset a checkbox thats checked value is set by php?

I have a web form with a bazillion check boxes. If the form is filled out incorrectly, none of the check boxes are reset (as an anti-frustration feature).

Problem is, the reset button doesn't work to uncheck all the boxes if the form has been submitted before. How do I fix this?

document.getElementById("form").reset() doesn't work if the form has been submitted beforehand either.

Sample code below:

<body>

<script>
function resetData() {
    document.getElementById("deleteContactFlag").checked=false;
    document.getElementById("userAdministrationFlag").checked=false;
}
</script>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" id="form">
    <input type="checkbox" name="deleteContactFlag" id="deleteContactFlag" <?php if (isset($_POST["deleteContactFlag"])) echo 'checked="checked"'; ?>/> Delete Contacts<br>
    <input name="userAdministrationFlag" type="checkbox" id="userAdministrationFlag" <?php if (isset($_POST["userAdministrationFlag"])) echo 'checked="checked"'; ?>/> User Administration<br><br>

    <input type="submit" name="submit" id="submit" value="Submit">
    <button type="button" onClick="resetData()">Reset</button>
</form>

</body>
Answer:1

The form reset does not clear all the values in the form, but sets the form back to it's initial state, which it had when the page first loaded. (In this initial state some checkboxes are already checked.)

Try resetting all checkboxes manually:

var inputs=document.getElementsByTagName("input");
for (var i in inputs)
    if (inputs[i].type=="checkbox") inputs[i].checked=false;

Or if you use jQuery:

$("input[type=checkbox]").each(function() { this.checked=false; });
Answer:2

Try to use Php code outside of the html elements

HTML

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"method="post" id="form">

    <input type="checkbox" name="deleteContactFlag" id="deleteContactFlag" /> Delete Contacts
    <input name="userAdministrationFlag" type="checkbox" id="userAdministrationFlag" /> User Administration

    <input type="submit" name="submit" id="submit" value="Submit">
    <button type="button" onClick="resetData()">Reset</button>
</form>
Answer:3

highcharts gets my hour time wrong I'm from venezuela just in case. I doing a real time system where I get in my db the time,seconds and miliseconds like 10:39:09:2 I apply the strtotime($time) then ...

highcharts gets my hour time wrong I'm from venezuela just in case. I doing a real time system where I get in my db the time,seconds and miliseconds like 10:39:09:2 I apply the strtotime($time) then ...

encodeURIComponent escapes all characters except the following: - _ . ! ~ * ' ( ) But is it possible to extend the functionality encode the above special characters as well. I know i can do ...

encodeURIComponent escapes all characters except the following: - _ . ! ~ * ' ( ) But is it possible to extend the functionality encode the above special characters as well. I know i can do ...

I ve drawn a pie chart graph using pie chart flot library.. I ve my own custom legend box. So I was trying to know if it is possible to hide the a particular slice based on user input. Like here Ive ...

I ve drawn a pie chart graph using pie chart flot library.. I ve my own custom legend box. So I was trying to know if it is possible to hide the a particular slice based on user input. Like here Ive ...

I am new in Javascript and I am trying to test following code <html> <head> <script src="jquery/jquery.min.js"> </head> <body> <input type="button" value="...

I am new in Javascript and I am trying to test following code <html> <head> <script src="jquery/jquery.min.js"> </head> <body> <input type="button" value="...

  1. element does not exist in cache
  2. element does not have an alt attribute
  3. element does not contain neutrons
  4. element does not follow octet rule
  5. element does not show allotropy
  6. element does not exist in mfs
  7. element does not exist in dom
  8. element does not show catenation
  9. element does not have a match in class
  10. element does not define a default property
  11. element does not exhibit allotropy
  12. element does not exist cypress
  13. element does not exist in selenium
  14. element doesn't have required attribute
  15. element doesn't exist in dom
  16. element doesn't float
  17. element doesn't exist on the window
  18. what element does not contain neutrons
  19. element doesn't have id
  20. element doesnt work