0

I have an checkbox form where users can select multiple boxes. now i want to get the value of each checked checkbox into one string.

this is what i have now!

            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="A"> Test 
            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="B"> Test 
            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="C"> Test
            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="D"> Test


    $keuzen = $_POST['keus'];

        foreach($keuzen as $keus) {
          $finalOptions = $keus;
        }

    echo $finalOptions;

the $finalOptions returns only the last checked box value...

when I select A & D $finalOptions says only D if i select C & A $finalOptions says only C etc...

someone who can help me please??

2
  • 1
    $finalOptions .= $keus;? Commented Sep 7, 2016 at 8:02
  • Thats because you overwrite your $finalOptions variable every iteration in the foreach loop Commented Sep 7, 2016 at 8:02

2 Answers 2

5

Try this:

<?php 
 $keuzen = $_POST['keus'];
 $finalOptions ="";

 foreach($keuzen as $keus) {
   $finalOptions .= $keus.","; // you may wanted to separate it with some delimiter
 }

 echo $finalOptions;
?>
Sign up to request clarification or add additional context in comments.

Comments

2

without looping it

<?php 
    $keuzen = $_POST['keus'];
    echo implode(',',$keuzen);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.