0

I'm not really sure if the Title was correct. The actual application is a quiz. Ok let me illustrate this and here's my sample snippet.

   <form method="POST"> 
    
    Softdrinks:
    <div class="checkbox">
       <label><input type="checkbox" name="question[1]answer[]" value="Coke">Coke</label>
    </div>
    <div class="checkbox">
       <label><input type="checkbox" name="question[1]answer[]" value="Royal">Royal</label>
    </div>
    
    Coffee :
    
    <div class="checkbox">
       <label><input type="checkbox" name="question[2]answer[]" value="Cappuccino">Cappuccino</label>
    </div>
    <div class="checkbox">
       <label><input type="checkbox" name="question[2]answer[]" value="Latte">Latte</label>
    </div>
    
    <button type="submit">Submit</button>
    
    </form>

Let's assume that all of the options are checked. But when the form submitted, The output was something like this:

"question":{
    "1":"Coke",
    "2":"Cappuccino"
}

So the "Royal" and "Latte" are skipped and i don't know why. I want the output should be like this :

"question":[
    "1": {
        "Coke",
        "Royal"
    }
    "2": {
        "Latte",
        "Cappuccino"
    }
]

1 Answer 1

1

The name attributes on the element are incorrect. Try this

<form method="POST"> 

Softdrinks:
<div class="checkbox">
   <label><input type="checkbox" name="question[1][]" value="Coke">Coke</label>
</div>
<div class="checkbox">
   <label><input type="checkbox" name="question[1][]" value="Royal">Royal</label>
</div>

Coffee :

<div class="checkbox">
   <label><input type="checkbox" name="question[2][]" value="Cappuccino">Cappuccino</label>
</div>
<div class="checkbox">
   <label><input type="checkbox" name="question[2][]" value="Latte">Latte</label>
</div>

<button type="submit">Submit</button>

</form>

it should result in the following output

"question":[
    "1": {
        "Coke",
        "Royal"
    }
    "2": {
        "Latte",
        "Cappuccino"
    }
]
Sign up to request clarification or add additional context in comments.

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.