I am trying to send data from dynamicly generated cheboxes(when ticked) to my php script, and then to mysql data base. I’ve got something like that:
form method="post" id="form">
<input type="checkbox" id="1" name="tick">
<input type="checkbox" id="2" name="tick">
<input type="checkbox" id="3" name="tick">
<input type="checkbox" id="4" name="tick">
<input type="checkbox" id="5" name="tick">
<input type="checkbox" id="6" name="tick">
</form>
(amount of checkboxes is not const!)
$(document).ready(function(){
$("form#form").check(function() {
var achieved = $('input[check]');
var id = $('input[id]');
var data = 'achieved=' + achieved.val() + '&id=' + id.val();
$.ajax({
type: "POST",
url: "change.php",
data: data,
success: function(){
$('div.changed').fadeIn(400);
setTimeout(function(){ $("div.changed").fadeOut(400) }, 4000);
}
});
return false;
});
});
What did I wrong?
You need to give your checkbox elements values (
value="something") you can check for on the server side.You can also use the
serialize()method to create the parameters string instead of doing it manually.The way you are doing it now is sure to not work because your
achievedvariable will not match any of your elements (did you mean:checkbox?) andidwill match all, so theval()will only return the firstvalue, which is not set in your example.Also, don’t use
&when you mean&. The encoded entity only makes sense in HTML.I’m also sure there is no
check()method in jQuery. Are you using a plugin ?