I know this sounds like a Nubian answer, but I am posting it here so that it can help others in the future.
Suppose you are building a table with a foreach loop. And at the same time adding flags at the end.
<!-- Begin Loop--> <tr> <td><?=$criteria?></td> <td><?=$indicator?></td> <td><?=$target?></td> <td> <div class="form-check"> <input type="checkbox" class="form-check-input" name="active" value="<?=$id?>" <?=$status?'checked':''?>> </div> </td> </tr>
You place the button under the table with hidden input:
<form method="post" action="/goalobj-review" id="goalobj"> <input type="hidden" name="result" id="selected" value="<?= $saved_data ?>> <button type="submit" class="btn btn-info" form="goalobj">Submit Changes</button> </form>
You can write your script like this:
<script type="text/javascript"> var checkboxes = document.getElementsByClassName('form-check-input'); var i; var tid = setInterval(function () { if (document.readyState !== "complete") { return; } clearInterval(tid); for(i=0;i<checkboxes.length;i++){ checkboxes[i].addEventListener('click',checkBoxValue); } },100); function checkBoxValue(event) { var selected = document.querySelector("input[id=selected]"); var result = 0; if(this.checked) { if(selected.value.length > 0) { result = selected.value + "," + this.value; document.querySelector("input[id=selected]").value = result; } else { result = this.value; document.querySelector("input[id=selected]").value = result; } } if(! this.checked) { </script>
The identifiers of your flags will be presented as a string "1,2" in the result variable. Then you can break it down at the controller level as you want.
Bruce Tong May 21 '19 at 12:26 2019-05-21 12:26
source share