javascript - checkbox data submit using ajax - Stack Overflow

<html><body><input type="checkbox" checked="checked"><body

<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
 |  Show 1 more ment

3 Answers 3

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

相关推荐

  • javascript - checkbox data submit using ajax - Stack Overflow

    <html><body><input type="checkbox" checked="checked"><body

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信