lundi 23 mai 2016

How do I get multiple values of of checked checkboxes at once with/without using foreach? Php

What I have here is a checkbox of days from Monday - Friday.

  <input type="checkbox" name="cb_day[]" id="cb_day" value="Monday">Monday     
  <input type="checkbox" name="cb_day[]" id="cb_day" value="Tuesday">Tuesday
  <input type="checkbox" name="cb_day[]" id="cb_day" value="Wednesday">Wednesday
  <input type="checkbox" name="cb_day[]" id="cb_day" value="Thursday">Thursday
  <input type="checkbox" name="cb_day[]" id="cb_day" value="Friday">Friday 
  <input type="checkbox" name="cb_day[]" id="cb_day" value="Saturday">Saturday

I get it that if you wanted to post values all checked checkboxes, use foreach. But that just selects the latest value.

  foreach($_POST['cb_day'] as $selected{
  echo "<p>" . $selected . "</p>";
  }else
  echo "Please select at least one option.";
  }

What I wanted to ask is: If I selected multiple values(ex. monday and tuesday), how should I code those multiple values as a single value(in this case: MT)?

EX: If I wanted to add another schedule

  [ ]monday         [/]thursday
  [/]tuesday        [ ]friday
  [ ]wednesday      [ ]saturday
  -----------------------------
  [/]monday         [/]thursday
  [ ]tuesday        [ ]friday
  [ ]wednesday      [ ]saturday

So for example I chose those, the output should be like this:

  TTH
  MTH

But the output in my code only showed the latest which is just:

  MTH

How do I catch those checked checkboxes and post it as a single value without losing the earlier values?

(Sorry for lengthy explanation. First time in stackoverflow.)




Aucun commentaire:

Enregistrer un commentaire