Jimmy Lin
Jimmy Lin

Reputation: 1501

Handling multiple select and same name of checkbox

I am having problems when I want to pass multiple value to backend from form.

Questions are:

  1. How can I get all values in the multiple select in the backend.
  2. How can I get the different value with same name in checkbox in the backend.

From the code in Jsfiddle, I'd like to show the select result in result.

By the way, I am using Django.

Full code is here: Jsfiddle

<form method="POST">
<select id="select-tag" name="tagging" multiple="multiple">
    <option value="">Tagging Sample... </option>
        <optgroup label="Type A">
            <option value="Aa">Aa</option>
            <option value="Ab">Ab</option>
            <option value="Ac">Ac</option>
    </optgroup>
    <optgroup label="TypeB">
        <option value="Ba">Ba</option>
        <option value="Bb">Bb</option>
        <option value="Bc">Bc</option>
    </optgroup>
</select>
<input type="checkbox"  value='00001' name="sample"></input>
<input type="checkbox"  value='00002' name="sample"></input>
<input type="submit" value="Submit" class="submitted_btn" name="btn_action"></input>
</form>

Upvotes: 0

Views: 942

Answers (1)

kp singh
kp singh

Reputation: 1460

You can use array type name in the html to get the value in backend.

<form method="POST">
<select id="select-tag" name="tagging[]" multiple="multiple">
    <option value="">Tagging Sample... </option>
    <optgroup label="Type A">
        <option value="Aa">Aa</option>
        <option value="Ab">Ab</option>
        <option value="Ac">Ac</option>
    </optgroup>
    <optgroup label="TypeB">
        <option value="Ba">Ba</option>
        <option value="Bb">Bb</option>
        <option value="Bc">Bc</option>
    </optgroup>
</select>
<input type="checkbox"  value='00001' name="sample[]"></input>
<input type="checkbox"  value='00002' name="sample[]"></input>
<input type="submit" value="Submit" class="submitted_btn" name="btn_action"></input>
</form>

Upvotes: 1

Related Questions