dongwei9365 2019-03-22 20:32
浏览 74
已采纳

PHP - 如何仅在循环中发布选中的复选框?

I am working on a project that signs up students to specific classes("french", "math",...). The student id is already selected. True a while loop i display my classes out of my database with a checkbox in front of each class. I only wanna post te checked checkboxes in a form that will send it to a php file thats puts the student and his selected classes into the database. But i cant seem to solve how i only post the checked checkboxes because of the difficulty with the while loop. I hope someone can help me. Already thanks!

  • 写回答

2条回答 默认 最新

  • duanhuilao0787 2019-03-25 12:41
    关注

    The best way would probably be to alter your HTML to POST the classes as an array instead of individual form elements.

    <checkbox name="classes[]" value="CLASS_ID_1">
    <checkbox name="classes[]" value="CLASS_ID_2">
    <checkbox name="classes[]" value="CLASS_ID_3">
    <checkbox name="classes[]" value="CLASS_ID_4">
    

    Then, the PHP side of this is very easy. Only the checked checkbox values will appear on the processing page.

    So, let's say that someone checks CLASS_ID_1, CLASS_ID_3 and CLASS_ID_4;

    print_r($_POST['classes']);
    

    The above code should return;

    array (
        "CLASS_ID_1",
        "CLASS_ID_3",
        "CLASS_ID_4"
    )
    

    So, essentially, it is an array of checked values. This means you do not need the class name or any additional information or loops to process the data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?