0

I have a form with only a checkbox:

<form action="escreve.php" method="post">
    <label><input type="checkbox" autocomplete="off" checked="checked" name="autosave">Autosave</label>
    <input type="submit" name="formSubmit" value="Submit" />
</form>

This will trigger escreve.php. It works fine, but the question is how can I trigger escreve.php without the submit button? It doesn't seem to make sense in this case since this form has only one input (the checkbox).

I'd like to have escreve.php reading the value of the checkbox whenever the user clicks in or out. Since I'm also using jQuery, would that be possible?

Thanks for any help!

4
  • Means you want to submit it on click of checkbox..?? Commented Aug 9, 2013 at 10:52
  • look up jquery ajax example (if you dont want to reload the page) or jquery sumbit form (if you want to refresh the page) Commented Aug 9, 2013 at 10:53
  • use ajax onclink event of the checkbox to save data Commented Aug 9, 2013 at 10:53
  • Yes, you can make a ajax call to escreve.php. Do you want to load the whole page again or just get values from the php file? Commented Aug 9, 2013 at 10:53

6 Answers 6

2

you can use some javascript to trigger the submit.

<form id='form' action="escreve.php" method="post">
    <label><input type="checkbox" autocomplete="off" checked="checked" name="autosave">Autosave</label>
</form>

<script>
$(document.ready(function (e) {
     $("input#autosave").click(function (e) {
         $("#form").submit();
     });
});
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

Use jquery form submit method

Try like this

<form action="escreve.php" method="post" id="myForm">
    <label><input type="checkbox" autocomplete="off" checked="checked" name="autosave" id="autosave">Autosave</label>
    <input type="submit" name="formSubmit" value="Submit" />
</form>

$(document).ready(function(){
  $("#autosave").click(function(){
      $("#myForm").submit()
  });
});

Comments

1

Don't get rid of the button, get rid of the checkbox.

<form action="escreve.php" method="post">
    <input type="submit" name="autosave" value="Autosave" />
</form>

4 Comments

+1. Don't break a user's expectation of an interface by causing a page change just by clicking on a checkbox. That's exactly what a button is for.
But what if the user wishes to have autosave, like draft save in SO or gmail. He may like to keep draft in his db. @Phylogenesis
@OptimusPrime The form very specifically has one control (the checkbox) that he wants to trigger a submit. Why not just have a submit button and follow UX guidelines?
@Phylogenesis Right, he shouldn't use checkbox for submit, but he is trying to have an autosave feature.
0

sure you can. see jQuery onChange() or onClick() and submit the form with jQuery with .submit()

Comments

0
   var form = $('form');  

  $('[name="autosave"]').click(function() {
        $.ajax({
          type: form.attr('method'),
          url: form.attr('action'),
          data: form.serialize()
        }).done(function() {
          // Optionally alert the user of success here...
        }).fail(function() {
          // Optionally alert the user of an error here...
        });
        event.preventDefault(); // Prevent the form from submitting via the browser.
    });

Comments

0
  • Give some id to your form
  • Then submit your form using submit() method on change event of checkbox

e.g

$('#chkid').change(function() { $('#frmid').submit(); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.