<html>
<body>
<input type="checkbox" checked="checked">
</body>
</html>
I want to pass the value of the checkbox as 1 or 0 according to selection. If it is checked then 1 is sent to a PHP file else 0 is sent. The value is sent using a onClick event in the html, using AJAX I want to send the value.
I'm new to AJAX so unable to figure out a way to acplish this task.
Thanks in advance.
<html>
<body>
<input type="checkbox" checked="checked">
</body>
</html>
I want to pass the value of the checkbox as 1 or 0 according to selection. If it is checked then 1 is sent to a PHP file else 0 is sent. The value is sent using a onClick event in the html, using AJAX I want to send the value.
I'm new to AJAX so unable to figure out a way to acplish this task.
Thanks in advance.
Share Improve this question asked Nov 9, 2012 at 4:20 user1731476user1731476 1253 silver badges10 bronze badges 6- I know that, but I don't know how to do it. – user1731476 Commented Nov 9, 2012 at 4:22
- 1 No i was asking you want the solution in jquery ? – coolguy Commented Nov 9, 2012 at 4:23
- It might be easier to change this on the PHP side (if at all possible)... – Lix Commented Nov 9, 2012 at 4:23
- 1 I'm pretty sure jquery page for .get gives you examples of how to do this. – Codeguy007 Commented Nov 9, 2012 at 4:24
- 2 I disagree the question is trival and answer is easily searched for. – Codeguy007 Commented Nov 9, 2012 at 4:38
3 Answers
Reset to default 3$(document).ready(function(){
$('body').delegate('#Yourbuttonid','click',function(){
var chkval = 0
if($('#yourcheckboxid').is(':checked')){
chkval = 1;
}
$.ajax({
url: "Your_url",
type: "POST",
data:{'checkboxvalue':chkval},
success:function(returndata){
},error:function(errordata){
}
});
});
});
and in your Php file
You can get the checkbbox value as echo $_POST['checkboxvalue'];
try
$('element').on('click', function(){
$checkbox = $(':checkbox').prop('checked');
$.post({
url :'PageWhereYouWanToSend.php',
data : { 'checked' : $checkbox },
success : function (resp){
alert(resp);
}
});
});
References:
.prop
.post
.on
var subcheck = 0;
$('input:checkbox').change(function(){
if($(this).is(':checked')){
subcheck = 1;
} else {
subcheck = 0;
}
$.post("your.php", { checkdata: subcheck},
function(data) {
//your code here
});
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745469124a4629051.html
评论列表(0条)