9

I'm trying to achieve an effect similar to 37signals' ta-da list - I want my users to be able to check off items from a list just by checking a "done" box - in other words a form gets submitted to the server on checking the box. Does anyone know of a tutorial which covers something like this, or could point me in the right direction?

Thanks Rob

3 Answers 3

15

its simple...

<input type="checkbox" onclick="this.form.submit();">
Sign up to request clarification or add additional context in comments.

2 Comments

Just what I was looking for.
I chose this method also - it may not be quite as slick as the AJAX approach but it means that my code will still work for people with JavaScript disabled and there are no possibilities of adding a new security vulnerability on the server where the AJAX code is calling.
4

If I understand your question correctly:

You could accomplish this using jQuery and AJAX. In the first example I'm doing it without submitting the whole form, and only submitting the value of the checkbox:

jQuery("#myCheckbox").click(function() {
   var $checkbox = jQuery(this);
   var checkboxData = "checkboxvalue=" + $checkbox.val();

   jQuery.ajax({
      url: "http://some.url.here",
      type: "POST",
      data: checkboxData,
      cache: false,
      dataType: "json",
      success: function(data) {
          if(data["success"]) {
            //do some other stuff if you have to
            //this is based on the assumption that you're sending back
            //JSON data that has a success property defined
          }
      }
   });
});

Presumably you'd have something on the server-side that handles the post.

If you actually do want to submit a form, you can do the same thing as above, except you'd serialize the form data:

jQuery("#myCheckbox").click(function() {
   var formData = jQuery("#formID").serialize();

   jQuery.ajax({
      url: "http://some.url.here",
      type: "POST",
      data: formData,
      cache: false,
      dataType: "json",
      success: function(data) {
          if(data["success"]) {
            //do some other stuff if you have to
            //this is based on the assumption that you're sending back
            //JSON data that has a success property defined
          }
      }
   });
});

Comments

0

<input type="checkbox" onclick="yourForm.submit()">
The above will submit the form when your checkbox is clicked... idk if thats what you want.
So it'll do the default form submit process...

2 Comments

But that will take him away from the page. I believe he wants to stay on the same page (he will, if he wants to check off the checkboxes one by one).
"a form gets submitted to the server on checking the box" =/ i assumed he meant form.submit()

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.