php - Ajax and checkbox array -
im new in using ajax , following tutorial link http://ricochen.wordpress.com/2011/08/02/passing-array-of-checkbox-values-to-php-through-jquery-example/
wasnt able make work can pls me on this?
html code :
<html> <head> <script src="jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> function doit() { var p=[]; $('input.cb').each( function() { if($(this).attr('checked')) { p.push($(this).attr('rel')); } } ); $.ajax( { url:'/test2.php', type:'post', data: {list:p}, success: function(res) { alert(res); } }); } </script> </head> <body> <input type="checkbox" class="cb" rel="1"></input>this 1<br /> <input type="checkbox" class="cb" rel="abc"></input>this abc<br /> <input type="checkbox" class="cb" rel="999"></input>this 999<br /> <a href="javascript:void(0)" onclick="doit()">click</a> </body> </html>
test2.php :
<?php print_r(@$_post['list']); ?>
try this:
1:you use <input></input>
wrong!!! <input type='checkbox' name='' />
2:you each checkbox
can each $('input.cb:checked')
, remove if
3:you want post ajax use $.post
<html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> function doit() { var p=[]; $('input.cb:checked').each(function(index,value) { p.push($(value).attr('rel')); }); $.post('/test2.php',{list:p}).done(function(res) {alert(res);}); } </script> </head> <body> <input type="checkbox" class="cb" rel="1"/>this 1<br /> <input type="checkbox" class="cb" rel="abc"/>this abc<br /> <input type="checkbox" class="cb" rel="999"/>this 999<br /> <a href="javascript:void(0)" onclick="doit()">click</a> </body> </html>
Comments
Post a Comment