Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

submitting a form when a checkbox is checked

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?

like image 643
user1394849 Avatar asked May 15 '12 14:05

user1394849


People also ask

How do you submit a form when a checkbox is checked?

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.

How can get checkbox value in submit?

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.

How do you submit form only once after multiple clicking on submit?

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.

How do I keep a checkbox checked in HTML?

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.


2 Answers

Use JavaScript by adding an onChange attribute to your input tags

<input onChange="this.form.submit()" ... /> 
like image 52
Sliq Avatar answered Oct 05 '22 21:10

Sliq


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.

like image 23
Surreal Dreams Avatar answered Oct 05 '22 19:10

Surreal Dreams