html - Add Multiple Choice Questions to PHP Web Form

I have a standard PHP web form with text fields, dropdowns, and radio buttons. When the form is submitted, the submitted information is e-mailed to me. I want to add a multiple choice question with checkboxes. I can produce the form field, but I can't figure out how to include multiple selections for that one question on the e-mail that gets sent when the form is submitted. Ideally, I would like to have the multiple selections on one line and separated by semi-colons (or commas if that's easier)

I've included what I think are the relevant parts of the code. Apologies if I didn't format this post correctly.

                <form  action="example.php" method="post" autocomplete="off">
                                    
                <div class="form-row">
                
            
                <label for="first">
                <span>First Name:</span>
                <input type="text" id="first"  name="first" autocomplete="off" list="autocompleteOff">
                </label>
                </div>
        
                <div class="form-row">
                <label for="last">
                <span>Last Name:</span>
                <input type="text" id="last"  name="last" autocomplete="off" list="autocompleteOff">
                </label>
                </div>
                
                <div class="form-row">
                <label for="email">
                <span>E-mail:</span>
                <input type="text" id="email"  name="email" autocomplete="off" list="autocompleteOff">
                </label>
                </div>
                
                <div class="form-row-multiple">
                <label for="multiple">
                <span>Choose all that apply:</span><br>
                <input type="checkbox" name="multiple" id="multiple" value="Option 1"><span>Option 1</span>
                <input type="checkbox" name="multiple" id="multiple" value="Option 2"><span>Option 2</span>
                <input type="checkbox" name="multiple" id="multiple" value="Option 3"><span>Option 3</span>                 
                <input type="checkbox" name="multiple" id="multiple" value="Option 4"><span>Option 4</span>                 </label>
                </div>  
               
                <div class="form-row">
                <button type="submit" name="submit" id="submit"><b>Submit</b></button>
                </div>
                    
                </form>



                <?php
                $first = remove_headers($_POST['first']);
                $last = remove_headers($_POST['last']);
                $email = remove_headers($_POST['email']);
                $multiple = remove_headers($_POST['multiple']);
                
                
                $message =
                "First: $first\n\n" .
                "Last: $last\n\n" 
                "E-mail: $email\n\n" 
                "Multiple: $multiple\n\n" 

Answer

Solution:

Two things to do here,

  1. For multiple checkboxes use the name attribute as an array and remove the id attribute, as an HTML page should have a unique ID throughout.

    <input type="checkbox" name="multiple[]" value="Option 1"><span>Option 1</span>
    <input type="checkbox" name="multiple[]" value="Option 2"><span>Option 2</span>
    <input type="checkbox" name="multiple[]" value="Option 3"><span>Option 3</span>
    <input type="checkbox" name="multiple[]" value="Option 4"><span>Option 4</span>
    
  2. When the form is submitted, multi-valued checkboxes are received as an array. So, we can make it semicolon-separated using the implode() function.

    $multiple = implode(';', $_POST['multiple']);
    

Source