I need to implement a button created in a view. In other programming languages this is very easy: your button has an ID so you can make a reference to it in a controller to implement its action. But in PHP I see like there are some predefined buttons (ex, submitbutton) and I don't understand how can you link an action with a button.
If someone could help me it would be very nice!
First you create an Action in your controller and then in your view try this:
<?= Html::a('YourFormName', ['yourControllerName/yourActionName'], ['class' => 'btn btn-success']) ?>
In an ActiveForm
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<?= Html::submitButton('Button caption', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
This is the code for submit button
<?= Html::submitButton('Button Name',['class'=>'btn btn-success'])?>
if you want to make button from link here is the code
<?= Html:a('Caption',['controller/action'],['class'=>'btn btn-success'])?>
if you want to pass some query string in link the her is the code
<?= Html::a('caption',['controller/action','id'=>$model->id],['class'=>'btn btn-success'])?>
Related
I have the following codes:
<?= $form->field($model, 'parent_id')->dropDownList($categories, ['class' => 'select']) ?>
<?= $form->field($model, "[{$lang['id']}]anchors",['template' => $template])->textarea(['class' => 'form-control editor']);?>
The first line is the dropdown list with multiple choices. Among the dropdown choices there is an option($model->parent_id ==10) which I would like to make it in a way that, when a user selects that option the second line to be inactive or hidden.
I tried to solve it with jquery but it didn't work.
Please share with me your solutions on this issue
Thank you in advance
Your Form
<?= $form->field($model, 'parent_id')->dropDownList($categories, ['id' => 'myParentField', 'prompt' => '--- Select Parent ---']) ?>
<div id="showField" style="display:none">
<?= $form->field($model, "[{$lang['id']}]anchors",['template' => $template])->textarea(['class' => 'form-control editor']);?>
</div>
Register Js in Your Form like below
<?php
$this->registerJs(<<<JS
$(document).ready(function(){
$('#myParentField').on('change', function() {
if (($('#myParentField').val()) == '10') {
$("#showField").show();
} else {
$("#showField").hide();
}
});
});
JS
);
?>
Refer Working with Client Scripts (Yii2 Register Js)
I need my signup form with email input to work wherever I want on the site.
I have an EmailsController that has add() and will add an email when I am on the add page.
How do I get the layout for Emails/Add.ctp to be injected into my home page and wherever I like while keeping the functionality and giving feedback to a user?
<div>
<?= $this->Element('upone_scriptimports'); ?>
<?= $this->Form->create() ?>
<?php echo $this->Form->control('email'); ?>
<?= $this->Form->button(__('Submit'), ['class'=>'btn btn-default text-right right']) ?>
<?= $this->Form->end() ?>
</div>
You will use elements, see CookBook
Put your code in here: src/Template/Element/Emails/add.ctp, you use your element like this echo $this->element('Emails/add');
Also add an action attribute to your form, specifying the controller
and action that will process the data.
<?= $this->Form->create(null, [
'url' => ['controller' => 'Articles', 'action' => 'publish']
]); ?>
<?= $this->Form->control('email'); ?>
<?= $this->Form->button(__('Submit'), [
'class' => 'btn btn-default text-right right'
]); ?>
<?= $this->Form->end(); ?>
See Setting a URL for the Form
I want to Create login and sign up on same page using CakeDC plugin but the Form->create variable is giving me errror
<?php echo $this->Form->create($register); ?>
because it not defined, i tried to give url for the register funtion but its not working...
<?php echo $this->Form->create($register,['url' => ['action' => 'register']]); ?>
Any Guidence...??
Also guide me the right way to use login, signup using CakeDC plugin in different pages in pop -up....
There is no real difference between a popup and a regular form. Use a form like this one
<?= $this->Form->create() ?>
<?= $this->Form->input('username', ['required' => true]) ?>
<?= $this->Form->input('password', ['required' => true]) ?>
<?= $this->Form->button(__d('CakeDC/Users', 'Login'), ['onclick' => '...']); ?>
<?= $this->Form->end() ?>
And create an onclick javascript ajax post to your login action.
My apologies for the vague title.
I'm having trouble coming up with a way to pass input from a textfield forward to an action I'm calling with the button on that view.
The code below is an ActiveForm I have on my view.
<div class="user-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'unique_key') ?>
<?= Html::a('Test', ['//order/create', 'unique_key' => 'entered key here'], ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
</p>
What I want to accomplish is that the user fills the text field with a (randomly generated) key.
Then, when they click the button, their entered key should be passed through to the order/create action. Problem is that I think it is impossible to get the entered key at this stage as I think it isn't saved anywhere yet.
I know this probably is wrong in so many different ways but I'm still trying to learn.
Is there any way I can accomplish what I'am trying to do ?
Thanks in advance
You simply need a better form config and a button to submit your form :
<?php $form = ActiveForm::begin([
'action' => '//order/create',
'method' => 'get',
]); ?>
<?= $form->field($model, 'unique_key') ?>
<?= Html::submitButton('Test', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
I've error on yii2 after I upgrade the os to el capitan. My project root is localhost/~robbyprawira/myproject/.
I create a link ,for example link .and when I click that link on browser it will go to localhost/home. it should be localhost/~robbyprawira/myproject/home
it happen to form action too.<form action="/admin/update"></form> .when I click the submit button,it will go to localhost/admin/update. should be localhost/~robbyprawira/myproject/admin/update
Anyone can help me fix this problem? thank you
<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
?>
<?php
$form = ActiveForm::begin(['action' => Url::to('/admin/update')]);
?>
<?= $form->field($model, 'admin_email', ['inputOptions' => ['title' => Yii::t('myproject/update','Email')]]) ?>
<?= Html::submitButton(Yii::t('myproject/update', 'Update'), ['class' => 'btn btn-lg btn-primary btn-block']) ?>
<?php ActiveForm::end(); ?>
Yii2 give you a lot of components for this situation without using direct code so you can
for the link the proper way is use URL helper
use yii\helpers\Url;
<a href="<?= Url::to('LINK')?>">
for the ancor tag you can use html Helper
use yii\helpers\Html;
<?= Html::a('Home', ['home'], ) ?>
for the form you can use active Form
$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'action' => Url::to(['/admin/update']),
]) ?>
......
<?php ActiveForm::end() ?>
Please try to use the following functions to make URLs:
For both at the top of the file:
use yii\helpers\Url;
Backend:
Url::to(Yii::getAlias('#web') . '/admin/update', true);
Frontend
Url::to(Yii::getAlias('#web') . '/home', true);
Does it work for you?