is there are way to submit a form when a checkbox is checked?
<form id="formName" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get"> <input type ="checkbox" name="cBox[]" value = "3">3</input> <input type ="checkbox" name="cBox[]" value = "4">4</input> <input type ="checkbox" name="cBox[]" value = "5">5</input> <input type="submit" name="submit" value="Search" /> </form> <?php if(isset($_GET['submit'])){ include 'displayResults.php'; } ?>
That is what I have currently, but I would like to submit the form without a submit button, when the user checks or unchecks a checkbox. Any help?
If you need to submit a form when a checkbox is checked or when it is unchecked like when you are using a switch, a good way is to create an hidden input. If you try to submit the checkbox argument if the checkbox is unchecked the form will not be submitted at all.
When it comes to using checkboxes, you kind of do not have a choice but to use foreach , and that's why you only get one value returned from your array. Here is an example using $_GET . You can however use $_POST and would need to make both directives match in both files in order to work properly.
onsubmit = function () { if (allowSubmit) allowSubmit = false; else return false; } })(); (well, as sure-fire as you can get with JS enabled anyway). You could disabled the button as a visual confirmation to the end user that the form can only be submit once too. Very correct.
The checked attribute is a boolean attribute. When present, it specifies that an <input> element should be pre-selected (checked) when the page loads. The checked attribute can be used with <input type="checkbox"> and <input type="radio"> . The checked attribute can also be set after the page load, with a JavaScript.
Use JavaScript by adding an onChange
attribute to your input tags
<input onChange="this.form.submit()" ... />
Yes, this is possible.
<form id="formName" action="<?php echo $_SERVER['PHP_SELF'];?>" method="get"> <input type ="checkbox" name="cBox[]" value = "3" onchange="document.getElementById('formName').submit()">3</input> <input type ="checkbox" name="cBox[]" value = "4" onchange="document.getElementById('formName').submit()">4</input> <input type ="checkbox" name="cBox[]" value = "5" onchange="document.getElementById('formName').submit()">5</input> <input type="submit" name="submit" value="Search" /> </form>
By adding onchange="document.getElementById('formName').submit()"
to each checkbox, you'll submit any time a checkbox is changed.
If you're OK with jQuery, it's even easier (and unobtrusive):
$(document).ready(function(){ $("#formname").on("change", "input:checkbox", function(){ $("#formname").submit(); }); });
For any number of checkboxes in your form, when the "change" event happens, the form is submitted. This will even work if you dynamically create more checkboxes thanks to the .on()
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With