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(); ?>
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
So I'm displaying the logged user's information on a page, followed by a form which can be used to change basic user info, such as e-mail address, username and so on.
In the ProfileController file, I have the Index action, which handles the user info and, theoretically, the form too:
public function actionIndex() {
$user = User::find()->where(['id' => Yii::$app->user->identity->id])->one();
if ($user->load(Yii::$app->request->post())) {
$user->save();
//var_dump($user->first_name);die;
}
return $this->render('index', [
'user' => $user,
]);
}
In the index view file, I'm displaying the form as it follows:
<div class="row form">
<div class="user-form col-md-8 col-md-offset-2">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($user, 'last_name')->textInput(['maxLength' => true]) ?>
<?= $form->field($user, 'first_name')->textInput(['maxLength' => true]) ?>
<?= $form->field($user, 'email')->textInput(['maxLength' => true]) ?>
<?= $form->field($user, 'image')->textInput([]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
Problem is, whenever I press the submit button beneath the form, the controller receives the post request, but does not update the new values given in the form fields, thus displaying the old info at the var_dump(). What should I do?
Probably validation failed. Proper way of doing this is:
// ...
if ($user->load(Yii::$app->request->post())) {
if ($user->save()) {
// validated and saved, check $user state now
}
}
Method save() calls method validate() automatically. You can call it on your own as well if you want to operate on this model only when it's validated.
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.
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'])?>