I am trying to check if button type submit is clicked, I have two buttons in the same form.
View sample
{!!Form::button('<i class="fa fa-trash-o"></i>', array('class' => 'btn btn-danger', 'type' => 'submit', 'name'=> 'movetrash'))!!}
I am not able to check with below
Input::get('movetrash').
This is how my condition looks like
if(Input::get('movetrash')) {
//do something is movetrash is clicked.
} else {
//perform other task
}
When I submit the form with Form::submit() method then it works great:
{!!Form::submit('Move to Trash',
array('class' => 'btn btn-default', 'name'=> 'movetrash'))!!}
but not with the below method (Form::button()). And I am unable to make it work with method shown below. All thought it also post the form data.
{!!Form::button('<i class="fa fa-trash-o"></i>',
array('class' => 'btn btn-danger', 'type' => 'submit', 'name'=> 'movetrash'))!!}
Related
I created signup form using with ActiveForm in yii2, by double click, form submits, it submits automaticaly. How do I prevent it, I want form submit just by click on submit button.
why this happens and what's the solution?
This is my form code:
$form = ActiveForm::begin([
'enableAjaxValidation' => true,
]);
?>
<?=$form->field($model, 'nickname', ['addon' => ['prepend' => ['content' => '<i class="glyphicon glyphicon-option-horizontal"></i>']]])->label(false)->textInput()?>
<?=$form->field($model, 'phone_number', ['addon' => ['prepend' => ['content' => '<i class="glyphicon glyphicon-option-horizontal"></i>']]])->label(false)->textInput()?>
<?=$form->field($model, 'email', ['addon' => ['prepend' => ['content' => '#']]])->label(false)->textInput()?>
<?=$form->field($model, 'password', [
'addon' => ['prepend' => ['content' => '<i class="fa fa-key"></i>']]])->passwordInput(['autocomplete' => 'off'])->label(false)->passwordInput();
?>
<div class="form-group">
<?=Html::submitButton(Yii::t('app', 'Signup'), ['value' => 'signupp', 'class' => 'btn btn-primary', 'name' => 'signupButton', 'style' => 'width:100%;'])?>
</div>
<?php ActiveForm::end();?>
You can use the ActiveForm's js event beforeSubmit to disable the button at the time the form is submitted, you can use CSS pointerEvents:'none' to disable the click on the button.
Assing an id="my-form" to your form and id="my-submit" to your submit button and use the below script on top of your view and your button will be disabled at the time of submission.
$js =<<< JS
$("#my-form").on("beforeSubmit",function(e){
e.preventDefault();
$("#my-submit").css({pointerEvents:'none'});
return true;
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
This view.php is generated with CRUD in Yii2 but the delete button doesn't confirm window.
But in index.php generated with CRUD in Yii2, The confirm window for delete worked.
<p>
<?= Html::a('Edit', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
<?= Html::a('Back','index', ['class' => 'btn btn-warning']) ?>
</p>
Delete Button in index.php:
<span class="glyphicon glyphicon-trash"></span>
Delete Button in view.php:
<a class="btn btn-danger" href="var/delete?id=2" data-confirm="Are you sure you want to delete this item?" data-method="post">Delete</a>
The section about yii.js is still in progress.
To get default support of confirmation you should register yii.js.
Using yii\web\YiiAsset
use yii\web\YiiAsset;
YiiAsset::register($this);
or registering script files (not recommend, just for example)
$this->registerJsFile('#yii/assets/yii.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
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>
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
I have a form in a cakephp view which saves well with a single button, here is the code in the view book_form.ctp
echo $this->Form->create
(
'Book',
array
(
'url' => array
(
'controller' => 'Books',
'action' => 'save_record'
),
'class' => 'span12 the_ajaxform',
'inputDefaults' => array
(
'label' => false,
'error' => false
)
)
);
.
.
// form fields
.
.
$options =array(
'label' => __('Save'),
'class' => 'btn btn-primary',
'id'=>'saveform'
);
echo $this->Form->end($options);
.
.
This works perfect! Now i wanted to add two buttons on that form and this is what i did
$options =array(array(
'label' => __('Save & Close'),
'class' => 'btn btn-primary',
'id'=>'saveform'
),
array(
'label' => __('Save & Create New'),
'class' => 'btn btn-primary',
'id'=>'saveformnew'
)
array(
'label' => __('Cancel'),
'class' => 'btn btn-primary',
'id'=>'formcancel'
));
echo $this->Form->end($options);
But this only brings one button which wont even submit the form,where am i going wrong?
and can each button call a different method in the controller?
Thanks in advance
If you set the name of the submit button, it will have that as a key in the post data, so you can redirect using that info at the start of your action. e.g.
<?php echo $this->Form->submit('btn1value', array('name'=>'btn1'))?>
<?php echo $this->Form->submit('btn2balue', array('name'=>'btn2'))?>
clicking the first button will give post data like:
array(
[btn1] => btn1value
[YourModel] => array(...)
)
Which makes it easy to do something like:
if (isset($this->request->data['btn1'])) {
// btn1 was clicked
} else if (isset($this->request->data['btn2'])) {
// btn2 was clicked
}
I am not sure whether it is "Technically Correct", HTML4, 5 compatible or not etc. but I have always done it something like this, without any problem so far:
<?php echo $this->Form->submit('Delete it', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Undelete Selected', array('name'=>'User[formaction]')); ?>
<?php echo $this->Form->submit('Purge Selected', array('name'=>'User[formaction]')); ?>
where "User" is the model name.
Usually one form can have just one action
this lmnitation is no longer true in HTML5 where you can set the form action for every button
so: the following code works only for HTML5 browsers
echo $this->Form->button(
'Your Action Description Here',
array(
'type' => 'submit',
'formaction' => 'yourActionHere' //
)
);
Try this, This is easy to do.
<div class="submit">
<?php echo $this->Form->submit(__('Submit', true), array('name' => 'ok', 'div' => false)); ?>
<?php echo $this->Form->button('Cancel', array('type' => 'button'));?>
Try using the FormHelper's button function to create the submit button and the other buttons and just call end after that without any options. This will output the buttons and end your form for you.
See: FormHelper::button
e.g.:
echo $this->Form->button('Save & Close', array('type' => 'submit'));
echo $this->Form->button('Save & Create New', array('type' => 'button'));
echo $this->Form->button('Cancel', array('type' => 'reset'));