lundi 18 septembre 2017

Yii2: How to get values from checkboxes and send it to controller?

I have a very simple HTML table inside a ActiveForm widget. It has two columns: Checkbox and Name. I send a form (a model from the view to the controller) and it works great. Now I need to send it with the id or the name of the selected (checked) rows.

This is the table:

<table class="table">
    <thead>
        <tr>
            <th>Checkbox</th><th>Name</th>
        </tr>
    </thead>
    <tbody id="tableBody">
    </tbody>
</table>

I have a JSON object to populate the table:

[
    {"idColor": 1, "name": "Blue"},
    {"idColor": 2, "name": "White"}
    {"idColor": 3, "name": "Orange"}
]

And here is the JavaScript code to populate the table:

for (var i = 0; i < json.length; i++) {
    myRows += "<tr><td><input type=checkbox value=" + json[i].idColor + "></td><td>" + json[i].name + "</td></tr>";
}
$("#tableBody").empty().append(myRows);




Aucun commentaire:

Enregistrer un commentaire