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>
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; });
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>
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="...