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 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题