Get POST data from multiple checkboxes?

前端 未结 3 1668
时光取名叫无心
时光取名叫无心 2020-12-06 16:40

Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.

I have a multiple checkbox option on my page...

3条回答
  •  一生所求
    2020-12-06 17:16

    Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:

    • Check if a certain value was selected:

       if (in_array("Other", $_POST['service'])) { /* Other was selected */}
      
    • Get a single newline-separated string with all selected options:

       echo implode("\n", $_POST['service']);
      
    • Loop through all selected checkboxes:

       foreach ($_POST['service'] as $service) {
           echo "You selected: $service 
      "; }

提交回复
热议问题