0

When I click on checkbox (either check it or uncheck it) I want to send its status(on/off) and value of id (or name) to php ( then to sql). My pseudocode is as follows:

  $("input:checkbox").click(function () {
      var cat_id = jQuery(this).attr('name');
      var status = [];

      if($("input:checkbox").is(':checked')) {
          return $(this).attr();
      } else {
          return $(this).attr();
      }

      $.post("var_dump.php", {
          cat_id: status
      }, // should be id(name of chbox) and its value 1-on, 0-off 
      function (response) {
          $("#result").text(response); //just for checking
      });

      return true;
  });

I will have many rows with data and check-box for each row (so unchecked box will not count ), I can use .map() or .each() to build an array but I want new values every time I change status of check-box - so every time it should be new $.post value.

Reason for array is that I want to make buttons to un-check five or ten (last) check-boxes/rows so it should be one time array. (but this will be implemented later.)

1 Answer 1

1

In your code the return statement will not even wait for post code to execute. Remove those unwanted return statements and try this. Also define status as a object instead of array since you want to send a key/value pair in the post request.

$("input:checkbox").click(function () {
      var cat_id = jQuery(this).attr('name');
      var status = {};
      //This will get the id or name whatever is available
      status[this.id || this.name] = this.checked ? "1": "0";

      $.post("var_dump.php", {
          cat_id: status
      }, // should be id(name of chbox) and its value 1-on, 0-off 
      function (response) {
          $("#result").text(response); //just for checking
      });

      return true;
  });
Sign up to request clarification or add additional context in comments.

Comments

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.