Manish Gupta
Manish Gupta

Reputation: 4666

HTML checkbox passing all values insted of selected

I have a form which pass values to django view.
In the form there is a dynamically generated table with a checkbox.

HTML:

<table class="table table-striped">
    <thead>
        <tr>
            <th><input type="checkbox" onClick="toggle(this, 'no')"></th>
            <th>Order Id</th>
            <th>Channel</th>
            <th>Dispatch By Date</th>
            <th>Amount</th>
            <th>Status</th>
            <th>Products</th>
        </tr>
    </thead>
    <tbody>
        {% for oid,oiid,dbd,stts,tp,sku,qty,chnl in new_orders %}
        <tr>
            <td><input type="checkbox" name="no" value="{{oid}}"></td>
            <td>
                <div><a class="btn-link" href="#">{{oid}}</a></div>
                <div><input name="oiid" style="display:none" value="{{oiid}}">{{oiid}}</div>
            </td>
            <td>{{chnl}}<input name="channel" style="display:none" value="{{chnl}}"></td>
            <td><span class="text-muted"><i class="fa fa-clock-o"></i>&nbsp;&nbsp;&nbsp;{{dbd}}</span></td>
            <td>{{tp}}</td>
            <td>{{stts}}</td>
            <td>
                <div>{{sku}}</div>
                <div>Quantity: {{qty}}</div>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>

I am passing 3 values back to my view. oid, oiid, chnl. The problem is if I select some elements instead of all, then the value of all oid is passed for those elements but the for oiid and chnl all the value are passed.

My Javascript:

function toggle(source, text) {
        checkboxes = document.getElementsByName(text);
        for(var i=0, n=checkboxes.length;i<n;i++) {
            checkboxes[i].checked = source.checked;
            }
        }

How to pass only those values which are selected for all three variables?

Upvotes: 1

Views: 166

Answers (1)

SatanicGeek
SatanicGeek

Reputation: 342

You must type all your inputs with type="checkbox" and name="YOURCHECKBOXNAME[]".

YOURCHECKBOXNAME should be the same.

When you put [] in a HTML input, the form will send to PHP an array.

Upvotes: 1

Related Questions