Ajax,复选框和& PHP

时间:2014-04-22 11:17:35

标签: javascript php ajax

需要一点帮助。 我有这个: enter image description here

允许用户选择多个项目,如果选择了我需要收集,在这种情况下,值是什么值是项目的标题。

我目前也有这个ajax:

  $('.box').on('change', function(event) {


         var valueName = $('#box').val();



            var checked = $('input[name=checked]:checked');
             if( checked.length > 0 ) {
                  var checkValues = checked.map(function(){
                        return $(this).val();
                    }).get();                        
                     $.ajax({
                          url: 'accessories_post.php',
                          type: 'post',
                          data: { checked: checkValues },
                          success:function(data){ console.log(data); }
                     });
                }

哪些帖子到accessories_post.php哪个

 print_r($_POST['checked']);

但是当我想抓住这些数据并放入变量时,我需要通过邮件发送任何想法,为什么我没有得到任何东西。

1 个答案:

答案 0 :(得分:2)

您可以简单地执行以下操作:

,而不是.map功能
 checkValues = '';
 $('input[name="checked"]:checked').each(function(){
     checkValues+=$(this).val()+",";
 });
 //this is to remove the extra comma , at the end of the string
 checkValues = checkValues.substring(0,(checkValues.length-1));

我已将这些复选框值连接起来,因此$_POST['checked']将返回 title,title2 等值。

关于您提出的id值的增量,您可以执行以下操作:

$i=0;
//your loop statement starts
 echo "<input type='checkbox' id='box{$i}' name='checked'>";
 $i++;
//loop ends