0

I'm trying for days to solve this problem, but with no results so far...
All I want to do is to POST checkbox value to php when the user check or uncheck it.

form.php:

<?php
if (isset($_POST['box'])) {
echo $_POST['box'];
} ?> 

Script:

<script>
$(document).ready(function() {
 $('.box').on('change', function(event) { 
  var checkbox = $(event.target);
  var isChecked = $(checkbox).is(':checked');
  $.post('/form.php', {id: checkbox.attr('id'), value: isChecked});
 });
}); 
</script> //Credit cymen

Form:

<form action="form.php" method="post">

<input id="box" class="box" name="box" type="checkbox" value="YESSSSSS!" />
<input id="box" class="box" name="box" type="checkbox" value="YESSSSSS!" />

</form>

(*) I get nothing on $_POST['box']

6
  • 3
    firstly your id's for each checkbox must be unique! Commented Feb 15, 2013 at 11:47
  • validate your html ... id must be unique Commented Feb 15, 2013 at 11:50
  • but I have a lot of them. How can php know which one I refer to? Commented Feb 15, 2013 at 11:51
  • @wizard Generally speaking the name attribute of the input corresponds to the request parameter that contains its value when you post the form. Commented Feb 15, 2013 at 11:51
  • @wizard, the element id is never sent to the server, only its name and value. For what you're doing here, there's no need for an id because you're using the class name to select the checkboxes. Commented Feb 15, 2013 at 11:54

3 Answers 3

3

Your JavaScript is submitting this data to PHP:

 {id: checkbox.attr('id'), value: isChecked}

You have two pieces of data there: "id" and "value". You don't have a piece of data called "box", so $_POST['box'] will not be populated.

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

2 Comments

Do you mean: {id: checkbox.attr('box'), value: isChecked}? try it - it doesn't work.
@wizard — No. You don't have <input box="anything"> do you? And that would still submit id something and you are looking for box in the data. If you want to submit the id as $_POST['box'] then {box:....
0

$_POST array that You sent to form.php is't look $_POST['box']. It looks like this:

array(2) {["id"]=>string(3) "box" ["value"]=> string(4) "true"}

So You can get to vars like thist $_POST['id'] or $_POST['value']. You're declaring vars in JS in this fragment: {id: checkbox.attr('id'), value: isChecked}.

It's good to check in PHP what You have in vars using var_dump, eg. var_dump($_POST).

Use Firebug plugin to FF, to debug AJAX.

Comments

0

I would change to:

$.post('/form.php', {name: checkbox.attr('name'), checked: isChecked, value : checkbox.val()});

On the PHP side you will receive:

Array
(
    [name] => box
    [checked] => true
    [value] => YESSSSSS!
)

You can access the POST data like:

echo $_POST['name']; // box
echo $_POST['checked']; // true/false
echo $_POST['value']; // YESSSSSS!

It's important that you include the checkbox value in the POST request because that is your only way of identifying which checkbox was changed.

1 Comment

Unfortunately it still not working....though everything seems in the right place.

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.