0

I need to send the values of the checkboxes in a page to a php page that will process this data and return a response in a <div> called result. This must get a response everytime a checkbox is checked or unchecked so the ajax call should work "live" for all the checkboxes in the page. I've tried all the examples found on SO, but without success so there must be something wrong in what I'm doing but I'm unable to find what.

This is one of the checkbox (all the checkboxes are similar)

echo "<input type='checkbox' id='model' class='model' value='" . $row["model"] . "'>" . $row["model"] . "<br>"; 

The following is the jquery I've tried

$("checkbox").click(function() {
   if(this.checked){
        $.ajax({
            type: "POST",
            url: 'pfinder.php',
            data: $(this).attr('value'), 
            success: function(data) {
                $("#result").load(result);
        },
        });

    }
};

I've also tried to use just the id of a single checkbox, just to test, but unsuccessfully

$("#model").click(function() {
       if(this.checked){
            $.ajax({
                type: "POST",
                url: 'pfinder.php',
                data: $(this).attr('value'), 
                success: function(data) {
                    $("#result").load(result);
                },
                });

            }
    };

And the following is pfinder.php that process the data

$model = $_POST["model"];
echo $model;

very basic, but so far, no data is showed in the <div id='result'> Any advice?

4
  • use $(this).checked instead of this.checked. Commented Jun 24, 2019 at 7:17
  • try to get value by $(this).val() or this.value Commented Jun 24, 2019 at 7:44
  • Your checkbox is single item or coming from inside of loop? Commented Jun 24, 2019 at 8:02
  • yes, the checkbox is generated in a while cycle that reads the data from a mysql db Commented Jun 25, 2019 at 6:16

2 Answers 2

1

Instead of this.checked try $(this).prop("checked").

and change $(this).attr('value') to $(this).val()

$(".model").click(function() {
   if($(this).prop("checked")){
        $.ajax({
            type: "POST",
            url: 'pfinder.php',
            data: $(this).val(), 
            success: function(data) {
                $("#result").load(result);
        },
        });

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

2 Comments

thank you for the answer, but unfortunately still not results. For entirety of information, the checkboxes are included in a form with post method. Do I have to add an id or a class to the form or it doesn't matter?
unfortunately it is still not working. I've added a ) after the last } because it was missing, but the div is still empty. I've also tried to further simplify my code, removing all the php and creating the checkbox with HTML only, and I've tried to insert the checkbox in a form, but still nothing. Any other advice?
0

I think you want to send the value of checkbox, if checkbox is checked and in case if checkbox is not checked you want to send 0. So here is the code which might be useful for you

$(document).on("change", ".model", function () {

    let c = this.checked ? this.value : '0'; // $(this).val()

    if(c){

      //check if it alerts when check box is checked

      alert('checkbox is checked');

      // perform your ajax here
      $.ajax({
        type: "POST",
        url: 'pfinder.php',
        data: $(this).val(), 
        success: function(data) {
            //check if ajax is completed of no
            alert('ajax completed');
            $("#result").load(result);
         },
      });

    }else{

      //check if it alerts when check box is unchecked
      alert('checkbox is NOT checked');
    }

});

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.