I need to create "bulk actions" similar to wordpress posts management, so you can for example delete multiple records at a time.
This is my approach, and works fine, but I'm sure it is not the best approach, since this method is vulnerable to CSRF hacks.
Checkbox column in a gridview:
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yiigridCheckboxColumn'],
'id'=>'grid',
'country',
],
]);
Button that fires a function
<a href="#" onclick="bulkAction('p');">
The function:
<script>
function bulkAction(a) {
var keys = $('#grid').yiiGridView('getSelectedRows');
window.location.href='<?php echo Url::to(['mycontroller/bulk']); ?>&action='+a+'&ids='+keys.join();
}
</script>
This function creates a url like this:
index.php?r=mycontroller/bulk&action=1&ids=2,6,7,8
PROBLEM IS
This approach is vulnerable to CSRF hacks (explained here: http://blog.codinghorror.com/cross-site-request-forgeries-and-you/)
So, what is the PROPER way to do it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…