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 form



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.










share|improve this question























  • 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














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 form



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.










share|improve this question























  • 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












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 form



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.










share|improve this question















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 form



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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());
); ',
]
]);





share|improve this answer






















  • 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











  • $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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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());
); ',
]
]);





share|improve this answer






















  • 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











  • $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














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());
); ',
]
]);





share|improve this answer






















  • 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











  • $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












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());
); ',
]
]);





share|improve this answer














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());
); ',
]
]);






share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • @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










  • 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










  • @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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus