Send post parameteres via yii2 Html a() - php

I'm trying to send parameteres via yii\bootstrap\Html;
echo Html::a('Создать', ['/orders/order-create','freelancer_group'=>$_GET['freelancer_group']], ['class'=>'btn btn-lg btn-primary','data'=>['method' => 'post','derp'=>'herp'],]);
But when I click on it is still var_dump($_POST['derp']); is NULL. How can I post it? I 've found it here

echo Html::a('Создать',
['/orders/order-create', 'freelancer_group' => $_GET['freelancer_group']],
[
'class' => 'btn btn-lg btn-primary',
'data' => [
'method' => 'post',
'params' => ['derp' => 'herp'], // <- extra level
],
]
);

Related

How to add a new line in Yii2 Data Confirm

I have a button using the following code in my view...
<?= Html::submitButton('Delete Summary', [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Really long message.<new line>Line 2<new line>Line 3?',
'method' => 'post',
],
]) ?>
I want to make the eventual confirmation dialog show my text on different lines, but none of the common notations such as \n, <br>, nor
work. I also tried putting \\n instead of \n, still it did not work.
How can I make the different parts of my confirmation show on a different line.
Just write as with the newline
<?= Html::submitButton('Delete Summary', [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Really long message.
Line 2
Line 3?',
'method' => 'post',
],
]) ;
?>
You can use \n is you define string using " quotes:
<?= Html::submitButton('Delete Summary', [
'class' => 'btn btn-danger',
'data' => [
'confirm' => "Really long message.\nLine 2\nLine 3?",
'method' => 'post',
],
]) ?>
You can also split it into multiple lines to improve readability:
<?= Html::submitButton('Delete Summary', [
'class' => 'btn btn-danger',
'data' => [
'confirm' => "Really long message."
. "\nLine 2"
. "\nLine 3?",
'method' => 'post',
],
]) ?>

Yii2 custom delete ActionColumn alert twice

I have a custom action column with just the delete button:
[
'class' => 'yii\grid\ActionColumn',
'template' => '{delete}',
'buttons' => [
'delete' => function($url, $data){
return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['orders-lines/delete', 'id' => $data->id], [
'data' => [
'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]);
}
]
],
and whenever i click the trash icon, then accept the alert box, the same alert box is triggered again so I have to accept twice the alert box.
What could i be missing?
Thanks
Try this
['class' => 'yii\grid\ActionColumn',
'template' => '{delete}',
'buttons' => [
'delete' => function ($url, $data) {
return Html::a('<span class="glyphicon glyphicon-trash" title="Delete"></span>', ['orders-lines/delete', 'id' => $data->id], ["data-pjax" => 0, 'onClick' => 'return confirm("Are you sure you want to delete this item?") ']);
},
],
],

YII2 Insert records selected from one grid to another

i'm new in Yii2, previously i use self-made-mvc-php + extjs 4.2
So i working on Yii2 now, i'm starting to get the hang of it for simple CRUD, my pobrem starts when i need to make entry form for parent-child tables.
I have 3 tables
commission
commission_id
commission_name
commission_amount
commission_percent
commission_scheme
cscheme_id
cscheme_name
cscheme_amount
cscheme_percent
cscheme_bonus
cscheme_description
commission_scheme_detail
cscheme_detail_id
commission_id
cscheme_id
The entry form is for commission_scheme and commission_scheme_detail.
I have generated all the crud using gii
i modify _form.php, add a gridview of commission_scheme_detail after the active form
i also add modal containing selection grid from commission
this is _form.php
gridview
<?php Pjax::begin(['id' => 'pjaxCschemedetail',
'timeout' => false,
'enablePushState' => false,
'clientOptions' => ['method' => 'POST']
]);
echo GridView::widget([
'id' => 'gridCschemedetail',
'dataProvider' => $detailData,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'CSCHEME_DETAIL_ID',
'CSCHEME_ID',
'COMMISSION_ID',
[
'class' => 'yii\grid\ActionColumn',
'buttons' => [
'update' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', Yii::$app->urlManager->createUrl(['commission-scheme-detail/view','id' => $model->CSCHEME_DETAIL_ID,'edit'=>'t']), [
'title' => Yii::t('yii', 'Edit'),
]);}
],
],
],
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
'panel' => [
'type'=>'info',
'before'=>
'after'=>Html::a('<i class="glyphicon glyphicon-repeat"></i> Reset List', ['index'], ['class' => 'btn btn-info']),
Html::button('Add', [
'id' => 'addDetailButton',
'class' => 'glyphicon glyphicon-plus btn btn-success btn-ajax-modal',
'value' => Url::to('#web/commission/listselect'),
'data-target' => '#modal_cschemedetail',
]),
'showFooter'=>false
],
]); Pjax::end();
Modal
Modal::begin([
'id' => 'modal_cschemedetail',
'header' => '<h4>Category</h4>',
]);
echo '<div id="modal-content"></div>';
echo Html::button('Add Selected', [
'id' => 'addCommissionsButton',
'class' => 'btn btn-success btn-ajax-modal'
]);
Modal::end();
?>
</div>
</div>
<div class="box-body">
</div>
</div>
selection grid
<?php Pjax::begin(); echo GridView::widget([
'id' => 'gridCommissionSelection',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'COMMISSION_ID',
'visible' => false
],
'COMMISSION_NAME',
[
'attribute' => 'TRANSACTION_TYPE_ID',
'value' => 'transactionTypeName'
],
'COMMISSION_DESC:ntext',
'COMMISSION_AMOUNT',
]
'responsive'=>true,
'hover'=>true,
'condensed'=>true,
'floatHeader'=>true,
]); Pjax::end(); ?>
Is it possible to insert the selected records from the modal to the detail gridview, then after user click the create button, the form and the inserted records in gridview saved to respective tables?
How can i insert the selected records to gridview (not to db table, it will be done after user click create)?
I suggest you to simplify your flow. You can easily save your models (performing an insert/update) at Modal and then refresh the page or call the $.pjax.reload('#gridCommissionSelection') to redraw the grid-view with new entries.
Another way is to append new lines with JS and then save them via AJAX request. It will be rather dirty and unclear solution, I guess :)

Action buttons only visible to admin yii2

I tried to add extra action buttons. Admin only view this button after button click update in a single field in the database.
<p>
<?php
if(!Yii::$app->user->isGuest ){
echo Html::a('Recommended', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']);
echo Html::a('Not Recommended', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to reject this application?',
'method' => 'post',
],
]);
} else if(Yii::$app->user->can('admin')){
}
?>
</p>
My problem is that I have 3 users that are: applicant, faculty and admin (or hod). In this case after faculty recommendation, the admin (or hod) sanctioned the leave.
I create leave application and faculty recommended, so now I want get the recommended data when admin login to the site.
If admin is the username you should follow this way :
<p>
<?php
if(!Yii::$app->user->isGuest ){
echo Html::a('Recommended', ['update', 'id' => $model->id],
['class' => 'btn btn-primary']);
echo Html::a('Not Recommended', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to reject this application?',
'method' => 'post',
],
]);
} else if(Yii::$app->user->identity->username == 'admin' ){
echo Html::a('Your Button Label for Admin',
['yourActionForAdmin', 'id' => $model->id],
['class' => 'btn btn-primary']);
}
?>
</p>

Laravel: Form buttons with names not showing in var_dump

I have 2 buttons with names neither of which show up in a var_dump($_POST) or print_r($_POST) on a form submit and I'm honestly confused as to why this would be happening. Is it a bug in Laravel or am I missing something? I'm using the latest Laravel version.
Here is my code:
{{ Form::open( array( 'id' => 'list-form', 'method' => 'POST', 'route' => 'user.admin.list' ) ) }}
{{ Form::button( 'Show List', array( 'type' => 'submit', 'name' => 'what', 'class' => 'btn btn-success' ) ) }}
{{ Form::button( 'Email List', array( 'type' => 'submit', 'name' => 'email_list', 'class' => 'btn btn-primary' ) ) }}
{{ Form::close() }}
Thank you for your help!
edit:
I've also just tried to do the following:
{{ Form::open( array( 'id' => 'list-form', 'method' => 'POST', 'route' => 'user.admin.list' ) ) }}
<input type="submit" value="Show List" name="show_list">
<input type="submit" value="Email List" name="email_list">
{{ Form::close() }}
But all I see in the output is just the form token and nothing else.
You are using Form::button(), but should be using Form::submit(). I just reproduced your form using two routes:
Route::any('form', ['as' => 'test', function()
{
return
// This one works
//
Form::open( array( 'id' => 'list-form', 'method' => 'POST', 'route' => 'user.admin.list' ) ) .
' <input type="submit" value="Show List" name="show_list">' .
'<input type="submit" value="Email List" name="email_list">' .
Form::close() .
// This one too
//
Form::open( array( 'id' => 'list-form', 'method' => 'POST', 'route' => 'user.admin.list' ) ) .
Form::submit( 'Show List', array( 'type' => 'submit', 'name' => 'show_list', 'class' => 'btn btn-success' ) ) .
Form::submit( 'Email List', array( 'type' => 'submit', 'name' => 'email_list', 'class' => 'btn btn-primary' ) ) .
Form::close() .
// This one doesn't
//
Form::open( array( 'id' => 'list-form', 'method' => 'POST', 'route' => 'user.admin.list' ) ) .
Form::button( 'Show List', array( 'type' => 'submit', 'name' => 'show_list', 'class' => 'btn btn-success' ) ) .
Form::button( 'Email List', array( 'type' => 'submit', 'name' => 'email_list', 'class' => 'btn btn-primary' ) ) .
Form::close() ;
}]);
Route::any('adminlist', ['as' => 'user.admin.list', function()
{
dd(Input::all());
}]);
In the first 2 you must receive something like:
array(2) {
["_token"] "TXlMuBczj4OmMEjOlkxusEhpUUZPBTqxQZHch2X2"
["email_list"] "Email List"
}
Also, you can use $_POST and $_GET in Laravel, but there's a better way:
Input::get('email_list');
If it doesn't work in your own code, you may have an HTML tag broken or a Javascript conflict. Try to debug it using your full raw HTML code.
A button is not part of a HTML form and is not send to the server.
Not really sure what you are trying to do here, with two buttons.
If you want a value in it, you will have to add that value to the $options array. But then again, what's the point of those multiple buttons?
From the spec:
The form element that the button is associated with (its form owner).
The value of the attribute must be the id attribute of a
element in the same document. If this attribute is not specified, the
element must be a descendant of a form element. This
attribute enables you to place elements anywhere within a
document, not just as descendants of their elements.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

Categories