Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
Pass checkbox value to php when i click on check box using ajax.
<html>
  <script src="jquery.js"></script>
         <script>
        $(document).ready(function(){
            $("#selectbox").on('click',function(){
                var checkbox = $(push(this).val()).(':checkbox').prop('checked');
                $.post({
                    url: 'checkboxPHP.php',
                    data: { 'checked':checkbox},
                    success:function(data){
                        ("#divcheck").html(data);
                    }
                })
            });
        })
</script>
<body>
Here is my checkbox code. I want to pass the value when i click on it.
 
<div id="prodnamelist"> foreach ($productname as $proname) {?&gt; 
&lt;input type="checkbox" name="productname" id="selectbox" class="productname" value="<?php echo $proname; ???>"  &gt;  <?php // echo $proname; ???> <br>
<?php  } ???>   </br></div>     
<div id="prodsizelist">  foreach ($productsize as $prosize) {  ?&gt;
&lt;input type="checkbox" name="productsize" id="selectbox" class="productsize" value="<?php echo $prosize; ???>"  &gt;  <?php // echo $prosize; ???> <br>
<?php }  ???>  </br></div></b>
Posted
Updated 27-Jan-16 0:14am
v4
Comments
Sinisa Hajnal 27-Jan-16 6:19am    
The code has several problems. First, your ID should be unique on the page, so looping through list of products without changing ID of the checkbox will cause problems with event registering. Either select by class name or name attribute or type, just change IDs to some unique value (i.e. product code or something)

Second, you read the value of the checkbox simply by using $(this).val() , you don't need additional checks and selectors, you already know which control is clicked because it is clicked :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900