how to get and display username in another form field when user select name from dropdownlist in yii2?
up vote
0
down vote
favorite
I want to display the lecturer username in another text input field form when the user selects the lecturer name in the drop-down list above.
this is my code for drop-down list lecturer name
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'fullname','fullname'),
['prompt'=>'Select Lecturer']
) ?>
this is my code for lecturer username
<?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
I want to get and display the LecUsername based on the selected drop-down list above. the LecUsername is from the database.
php yii2 dropdown
add a comment |
up vote
0
down vote
favorite
I want to display the lecturer username in another text input field form when the user selects the lecturer name in the drop-down list above.
this is my code for drop-down list lecturer name
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'fullname','fullname'),
['prompt'=>'Select Lecturer']
) ?>
this is my code for lecturer username
<?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
I want to get and display the LecUsername based on the selected drop-down list above. the LecUsername is from the database.
php yii2 dropdown
See yiiframework.com/wiki/723/…
– Anton Rybalko
Nov 9 at 13:09
See demos.krajee.com/widget-details/depdrop
– Anton Rybalko
Nov 9 at 13:13
Possible duplicate of dependent dropdown yii2. How to do?
– Anton Rybalko
Nov 9 at 13:14
@AntonRybalko thank you so much but i dont want to use another widget if possible.
– beginnerQueen
Nov 9 at 13:39
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to display the lecturer username in another text input field form when the user selects the lecturer name in the drop-down list above.
this is my code for drop-down list lecturer name
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'fullname','fullname'),
['prompt'=>'Select Lecturer']
) ?>
this is my code for lecturer username
<?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
I want to get and display the LecUsername based on the selected drop-down list above. the LecUsername is from the database.
php yii2 dropdown
I want to display the lecturer username in another text input field form when the user selects the lecturer name in the drop-down list above.
this is my code for drop-down list lecturer name
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'fullname','fullname'),
['prompt'=>'Select Lecturer']
) ?>
this is my code for lecturer username
<?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
I want to get and display the LecUsername based on the selected drop-down list above. the LecUsername is from the database.
php yii2 dropdown
php yii2 dropdown
edited Nov 9 at 13:43
Sayed Mohd Ali
1539
1539
asked Nov 9 at 12:48
beginnerQueen
123
123
See yiiframework.com/wiki/723/…
– Anton Rybalko
Nov 9 at 13:09
See demos.krajee.com/widget-details/depdrop
– Anton Rybalko
Nov 9 at 13:13
Possible duplicate of dependent dropdown yii2. How to do?
– Anton Rybalko
Nov 9 at 13:14
@AntonRybalko thank you so much but i dont want to use another widget if possible.
– beginnerQueen
Nov 9 at 13:39
add a comment |
See yiiframework.com/wiki/723/…
– Anton Rybalko
Nov 9 at 13:09
See demos.krajee.com/widget-details/depdrop
– Anton Rybalko
Nov 9 at 13:13
Possible duplicate of dependent dropdown yii2. How to do?
– Anton Rybalko
Nov 9 at 13:14
@AntonRybalko thank you so much but i dont want to use another widget if possible.
– beginnerQueen
Nov 9 at 13:39
See yiiframework.com/wiki/723/…
– Anton Rybalko
Nov 9 at 13:09
See yiiframework.com/wiki/723/…
– Anton Rybalko
Nov 9 at 13:09
See demos.krajee.com/widget-details/depdrop
– Anton Rybalko
Nov 9 at 13:13
See demos.krajee.com/widget-details/depdrop
– Anton Rybalko
Nov 9 at 13:13
Possible duplicate of dependent dropdown yii2. How to do?
– Anton Rybalko
Nov 9 at 13:14
Possible duplicate of dependent dropdown yii2. How to do?
– Anton Rybalko
Nov 9 at 13:14
@AntonRybalko thank you so much but i dont want to use another widget if possible.
– beginnerQueen
Nov 9 at 13:39
@AntonRybalko thank you so much but i dont want to use another widget if possible.
– beginnerQueen
Nov 9 at 13:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example.
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')
so your dropdown code will look like
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
['prompt'=>'Select Lecturer']
) ?>
then add the below on top of your view file where you have the form
$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');
$js = <<< JS
$(document).ready(function()
$("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
);
);
JS;
$this->registerJs($js, yiiwebView::POS_READY);
A better way for dropdowns is to use the library Kartik-vselect2 and use the event pluginEvents option to specify the event select2:select, your code should look like below in that case.
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() $("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
); ',
]
]);
what should i put here ? <?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
– beginnerQueen
Nov 9 at 13:37
this stays the same you dont need to change anything for theLecUsername@queenhoney
– Muhammad Omer Aslam
Nov 9 at 13:38
$ddId=Html::getInputId($model, 'LecturerName'); $inputId=Html::getInputId($model, 'LecUsername'); $js = <<< JS $(document).ready(function() $("#$ddId").on('change',function(e) $("#$inputId").val($(this).val()); ); ); JS; did you mean i need to put this code inside view.php ? i already put it but nothing happen
– beginnerQueen
Nov 9 at 14:15
@beginnerQueen yes, sorry i missed a line to register the script$this->registerJs($js, yiiwebView::POS_READY);i updated the answer above , it should work now
– Muhammad Omer Aslam
Nov 9 at 14:32
Thank you so much ! its working :)
– beginnerQueen
Nov 9 at 14:42
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example.
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')
so your dropdown code will look like
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
['prompt'=>'Select Lecturer']
) ?>
then add the below on top of your view file where you have the form
$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');
$js = <<< JS
$(document).ready(function()
$("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
);
);
JS;
$this->registerJs($js, yiiwebView::POS_READY);
A better way for dropdowns is to use the library Kartik-vselect2 and use the event pluginEvents option to specify the event select2:select, your code should look like below in that case.
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() $("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
); ',
]
]);
what should i put here ? <?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
– beginnerQueen
Nov 9 at 13:37
this stays the same you dont need to change anything for theLecUsername@queenhoney
– Muhammad Omer Aslam
Nov 9 at 13:38
$ddId=Html::getInputId($model, 'LecturerName'); $inputId=Html::getInputId($model, 'LecUsername'); $js = <<< JS $(document).ready(function() $("#$ddId").on('change',function(e) $("#$inputId").val($(this).val()); ); ); JS; did you mean i need to put this code inside view.php ? i already put it but nothing happen
– beginnerQueen
Nov 9 at 14:15
@beginnerQueen yes, sorry i missed a line to register the script$this->registerJs($js, yiiwebView::POS_READY);i updated the answer above , it should work now
– Muhammad Omer Aslam
Nov 9 at 14:32
Thank you so much ! its working :)
– beginnerQueen
Nov 9 at 14:42
|
show 3 more comments
up vote
1
down vote
accepted
As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example.
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')
so your dropdown code will look like
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
['prompt'=>'Select Lecturer']
) ?>
then add the below on top of your view file where you have the form
$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');
$js = <<< JS
$(document).ready(function()
$("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
);
);
JS;
$this->registerJs($js, yiiwebView::POS_READY);
A better way for dropdowns is to use the library Kartik-vselect2 and use the event pluginEvents option to specify the event select2:select, your code should look like below in that case.
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() $("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
); ',
]
]);
what should i put here ? <?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
– beginnerQueen
Nov 9 at 13:37
this stays the same you dont need to change anything for theLecUsername@queenhoney
– Muhammad Omer Aslam
Nov 9 at 13:38
$ddId=Html::getInputId($model, 'LecturerName'); $inputId=Html::getInputId($model, 'LecUsername'); $js = <<< JS $(document).ready(function() $("#$ddId").on('change',function(e) $("#$inputId").val($(this).val()); ); ); JS; did you mean i need to put this code inside view.php ? i already put it but nothing happen
– beginnerQueen
Nov 9 at 14:15
@beginnerQueen yes, sorry i missed a line to register the script$this->registerJs($js, yiiwebView::POS_READY);i updated the answer above , it should work now
– Muhammad Omer Aslam
Nov 9 at 14:32
Thank you so much ! its working :)
– beginnerQueen
Nov 9 at 14:42
|
show 3 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example.
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')
so your dropdown code will look like
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
['prompt'=>'Select Lecturer']
) ?>
then add the below on top of your view file where you have the form
$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');
$js = <<< JS
$(document).ready(function()
$("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
);
);
JS;
$this->registerJs($js, yiiwebView::POS_READY);
A better way for dropdowns is to use the library Kartik-vselect2 and use the event pluginEvents option to specify the event select2:select, your code should look like below in that case.
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() $("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
); ',
]
]);
As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example.
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')
so your dropdown code will look like
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
['prompt'=>'Select Lecturer']
) ?>
then add the below on top of your view file where you have the form
$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');
$js = <<< JS
$(document).ready(function()
$("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
);
);
JS;
$this->registerJs($js, yiiwebView::POS_READY);
A better way for dropdowns is to use the library Kartik-vselect2 and use the event pluginEvents option to specify the event select2:select, your code should look like below in that case.
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() $("#$ddId").on('change',function(e)
$("#$inputId").val($(this).val());
); ',
]
]);
edited Nov 9 at 14:31
answered Nov 9 at 13:08
Muhammad Omer Aslam
11.5k62243
11.5k62243
what should i put here ? <?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
– beginnerQueen
Nov 9 at 13:37
this stays the same you dont need to change anything for theLecUsername@queenhoney
– Muhammad Omer Aslam
Nov 9 at 13:38
$ddId=Html::getInputId($model, 'LecturerName'); $inputId=Html::getInputId($model, 'LecUsername'); $js = <<< JS $(document).ready(function() $("#$ddId").on('change',function(e) $("#$inputId").val($(this).val()); ); ); JS; did you mean i need to put this code inside view.php ? i already put it but nothing happen
– beginnerQueen
Nov 9 at 14:15
@beginnerQueen yes, sorry i missed a line to register the script$this->registerJs($js, yiiwebView::POS_READY);i updated the answer above , it should work now
– Muhammad Omer Aslam
Nov 9 at 14:32
Thank you so much ! its working :)
– beginnerQueen
Nov 9 at 14:42
|
show 3 more comments
what should i put here ? <?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
– beginnerQueen
Nov 9 at 13:37
this stays the same you dont need to change anything for theLecUsername@queenhoney
– Muhammad Omer Aslam
Nov 9 at 13:38
$ddId=Html::getInputId($model, 'LecturerName'); $inputId=Html::getInputId($model, 'LecUsername'); $js = <<< JS $(document).ready(function() $("#$ddId").on('change',function(e) $("#$inputId").val($(this).val()); ); ); JS; did you mean i need to put this code inside view.php ? i already put it but nothing happen
– beginnerQueen
Nov 9 at 14:15
@beginnerQueen yes, sorry i missed a line to register the script$this->registerJs($js, yiiwebView::POS_READY);i updated the answer above , it should work now
– Muhammad Omer Aslam
Nov 9 at 14:32
Thank you so much ! its working :)
– beginnerQueen
Nov 9 at 14:42
what should i put here ? <?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
– beginnerQueen
Nov 9 at 13:37
what should i put here ? <?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
– beginnerQueen
Nov 9 at 13:37
this stays the same you dont need to change anything for the
LecUsername @queenhoney– Muhammad Omer Aslam
Nov 9 at 13:38
this stays the same you dont need to change anything for the
LecUsername @queenhoney– Muhammad Omer Aslam
Nov 9 at 13:38
$ddId=Html::getInputId($model, 'LecturerName'); $inputId=Html::getInputId($model, 'LecUsername'); $js = <<< JS $(document).ready(function() $("#$ddId").on('change',function(e) $("#$inputId").val($(this).val()); ); ); JS; did you mean i need to put this code inside view.php ? i already put it but nothing happen
– beginnerQueen
Nov 9 at 14:15
$ddId=Html::getInputId($model, 'LecturerName'); $inputId=Html::getInputId($model, 'LecUsername'); $js = <<< JS $(document).ready(function() $("#$ddId").on('change',function(e) $("#$inputId").val($(this).val()); ); ); JS; did you mean i need to put this code inside view.php ? i already put it but nothing happen
– beginnerQueen
Nov 9 at 14:15
@beginnerQueen yes, sorry i missed a line to register the script
$this->registerJs($js, yiiwebView::POS_READY); i updated the answer above , it should work now– Muhammad Omer Aslam
Nov 9 at 14:32
@beginnerQueen yes, sorry i missed a line to register the script
$this->registerJs($js, yiiwebView::POS_READY); i updated the answer above , it should work now– Muhammad Omer Aslam
Nov 9 at 14:32
Thank you so much ! its working :)
– beginnerQueen
Nov 9 at 14:42
Thank you so much ! its working :)
– beginnerQueen
Nov 9 at 14:42
|
show 3 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226001%2fhow-to-get-and-display-username-in-another-form-field-when-user-select-name-from%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
See yiiframework.com/wiki/723/…
– Anton Rybalko
Nov 9 at 13:09
See demos.krajee.com/widget-details/depdrop
– Anton Rybalko
Nov 9 at 13:13
Possible duplicate of dependent dropdown yii2. How to do?
– Anton Rybalko
Nov 9 at 13:14
@AntonRybalko thank you so much but i dont want to use another widget if possible.
– beginnerQueen
Nov 9 at 13:39