0

I have a list of checkboxes, and want to submit the value of the checkbox when it is checked to PHP. I would prefer not to have a submit button, and rather post the value each time a checkbox is checked, so that the form does not refresh and clear the checked checkbox. However, each time I click one of the checkboxes, nothing happens.

Here is the HTML:

<form method="post" action="this.form.submit();" name="form1">
        <input type="checkbox" name="checkboxList[]" onclick="function1();" value="1">value1</input>
        <input type="checkbox" name="checkboxList[]" onclick="function1();" value="2">value2</input>
        <input type="checkbox" name="checkboxList[]" onclick="function1();" value="3">value3</input>
</form>

Here is the JQuery:

function function1() {
    var data = $("form1").serialize();
    $.ajax({
        url: "test.php", 
        type: "POST",
        async: true,
        cache: false,
        data: data, 
        success: function(data){ 
            alert(data) 
        }
    });
}

Edit: Sorry, forgot to mention that I have included the JQuery library.

And "test.php":

print_r($_POST);
3
  • Have you used the browsers javascript debugger (F12) to see what does happen? Commented Sep 18, 2016 at 20:36
  • Where is the PHP script called test.php Commented Sep 18, 2016 at 20:38
  • You haven't added the jQuery Library it seems. Put this in your code at the top: <script src="https://code.jquery.com/jquery-1.12.4.js"></script> Commented Sep 18, 2016 at 20:39

3 Answers 3

1

Instead of var data = $("form1").serialize(); you should use var data = $("[name='form1']").serialize(). Using a plain string like that won't find you anything. You must specify that you want the element with the name, 'form1'

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

Comments

0

You need to use onchange event not onclick event https://api.jquery.com/change/

2 Comments

onclick is successfully calling function1()
that may well be the case, but for the submission, you need the change event. see below, Kostandy did it for you.
-1

Try this:

<form method="post" name="form1">
        <input type="checkbox" name="checkboxList[]" value="1">value1</input>
        <input type="checkbox" name="checkboxList[]" value="2">value2</input>
        <input type="checkbox" name="checkboxList[]" value="3">value3</input>
</form>

JQuery:

$("input[type=checkbox]").on("change", function() {
    var data = $("form1").serialize();
    $.ajax({
        url: "test.php", 
        type: "POST",
        async: true,
        cache: false,
        data: ({data: data}),
        dataType: "text", 
        success: function(data){ 
            alert(data) 
        }
    });
});

1 Comment

Why should the OP "use this code"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer.

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.