dongraa1986 2015-07-13 16:56
浏览 43

使用PHP循环通过AJAX发布的复选框数组

I've spent quite a bit of time looking up how I could do this. Basically, I have a few checkboxes that I am sending through AJAX to a PHP script, and I want the PHP script to echo the value of each selected checkbox.

Here is the code for my checkboxes:

while ($template = $stmt->fetch(PDO::FETCH_ASSOC)){
    echo("<input class='checkbox templateSelectBox' name='RelatedTemplates[]' data-template='". $template['ID'] ."' value='".$template['ID']."' type='checkbox'> <p class='label label-default'>". $template['Name'] ."</p><br>");
}

With that PHP code, there are 3 checkboxes on the page. Here is the AJAX that sends the checked checkboxes:

$.ajax({
   type: 'POST',
   url: 'inc/ajax/addserver.ajax.php',
   data: {'SelectedTemplates': $(".templateSelectBox:checked").serialize()},
   success: function(data){
      $("#responses").html(data);
   }  })

Then say if I check the second checkbox and echo it using echo($_POST['SelectedTemplates']), it echoes RelatedTemplates%5B%5D=2, which I don't fully understand why. All I'm expecting is the value of the checkbox, which is only a single digit. Instead I'm getting that string.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么