radio button and values

Till here everything looks good…

import wixData from 'wix-data';

var DBFIELDS = [], ITEMS1, ITEMS2 //<-- ATTENTION HERE ! ! !
//-------- USER-INTERFACE -------------------------------------------------
var txtFieldID1 = "#textBox1"
var txtFieldID2 = "#textBox6" //<-- defined a second Text-Field

var DD_ID1 = "#dropdown1" 
var DD_ID2 = "#dropdown2" //<-- defined a second dropdown

var DATABASE = "RegistrationAgeandSkill" 
var DBLIMIT = 1000 

var OUTPUT_FIELD1 = "ageValue"
var OUTPUT_FIELD2 = "skillValue" //<-- defined a second OUTPUT

 DBFIELDS[0] = "age" 
 DBFIELDS[1] = "skill" //<-- defined a second DB-Field
//-------- USER-INTERFACE -------------------------------------------------

What happens here?

  await wixData.query(DATABASE)
  .limit(DBLIMIT).find()
  .then(results=> {
    ITEMS1 = results.items 
    create_UniqueDropdown1(ITEMS1);
    //create_UniqueDropdown2(ITEMS2);
 });

A DABASE gets queried and as output you get some results (all results of DB).
Then you send these results to the function → “create_UniqueDropdowns”. This function will (like the name already says) populate a dropdown with unique values from the queried DB.

Here the function recieves the RESULT_DATA…(items)

function create_UniqueDropdown1(items) {
    const uniqueTitles1 = getUniqueTitles1(items);
    $w(DD_ID1).options = buildOptions1(uniqueTitles1); 
 
    function getUniqueTitles1(items) {
       const titlesOnly = items.map(item => item[DBFIELDS[0]]); 
       return [...new Set(titlesOnly)];
    }

    function buildOptions1(uniqueList1) {
       return uniqueList1.map(curr => {
          return {label:curr, value:curr};
       });
    }
}

and populates a dropdown —> using → DBFIELDS[0] → “age”

Yes, now you need a second populate-dropdown part…

function  create_UniqueDropdown2(items) { 
   const uniqueTitles2 = getUniqueTitles2(items);  
   $w(DD_ID2).options = buildOptions2(uniqueTitles2);  
   function getUniqueTitles2(items) { 
     const titlesOnly = items.map(item => item[DBFIELDS[0]]);  
     return [...new  Set(titlesOnly)]; 
   } 
   function buildOptions2(uniqueList2) {  
     return uniqueList2.map(curr => { 
        return {label:curr, value:curr}; 
     }); 
   } 
}  

… which will take the same DB-RESULTS but will have another IN & OUTPUT.
INPUT —> DBFIELDS[1] = “skill”
OUTPUT —> var OUTPUT_FIELD2 = “skillValue”

Your last step, would be also to add a code-part, which should be able to start all these actions when do changes in the second dropdown.

$w(DD_ID2).onChange(()=>{
   let INDEX = $w(DD_ID2).selectedIndex
   $w(txtFieldID2).value = ITEMS2[INDEX[OUTPUT_FIELD2];
});

Or better sayed, which will take the results of the already queried DB-RESULTS.

You choose → dropdown1 → ITEMS1 will be loaded.
You choose → dropdown2 → ITEMS2 will be loaded.

Additional stuff/info… (regarding GIRI-ZANO 's answers)

This post here will help you to understand another very important coding-lesson.

You have the same situation in your own code.

And right now → you are on your own. You have now all the needed infos, to get the rest on your own and get it to work.

If you have still difficulties → step-back or even restart reading from beginning of this post.

Also try to find additional infos to your own issue in the WWW.

Good luck! :wink:

great explaination of everything, thanking for taking the time to explain…here is what i came up with…

import wixData from ‘wix-data’ ;

var DBFIELDS = [], ITEMS ;

//-------- USER-INTERFACE -------------------------------------------------
var txtFieldID1 = “#textBox1
var txtFieldID2 = “#textBox6

var DD_ID1 = “#dropdown1
var DD_ID2 = “#dropdown2

var DATABASE = “RegistrationAgeandSkill”
var DBLIMIT = 1000

var OUTPUT_FIELD1 = “ageValue”
var OUTPUT_FIELD2 = “skillValue”

DBFIELDS [ 0 ] = “age”
DBFIELDS [ 1 ] = “skill”
//-------- USER-INTERFACE -------------------------------------------------

$w . onReady ( async ()=>{
await wixData . query ( DATABASE )
. limit ( DBLIMIT ). find ()
. then ( results => {
ITEMS = results . items
create_UniqueDropdown 1 ( ITEMS );
});

$w ( DD_ID1 ). onChange (()=>{
let INDEX = $w ( DD_ID1 ). selectedIndex
$w ( txtFieldID1 ). value = ITEMS [ INDEX ][ OUTPUT_FIELD1 ];
});
});

function create_UniqueDropdown1 ( items ) {
const uniqueTitles1 = getUniqueTitles1 ( items );
$w ( DD_ID1 ). options = buildOptions1 ( uniqueTitles1 );

function getUniqueTitles1 ( items ) {
const titlesOnly = items . map ( item => item [ DBFIELDS [ 0 ]]);
return [… new Set ( titlesOnly )];
}

function buildOptions1 ( uniqueList1 ) {
return uniqueList1 . map ( curr => {
return { label : curr , value : curr };
});
}
}
function create_UniqueDropdown2 ( items ) {
const uniqueTitles2 = getUniqueTitles2 ( items );
$w ( DD_ID2 ). options = buildOptions2 ( uniqueTitles2 );

function getUniqueTitles2 ( items ) {
const titlesOnly = items . map ( item => item [ DBFIELDS [ 1 ]]);
return [… new Set ( titlesOnly )];
}

function buildOptions2 ( uniqueList2 ) {
return uniqueList2 . map ( curr => {
return { label : curr , value : curr };
});
}
}

$w ( DD_ID2 ). onChange (()=>{
let INDEX = $w ( DD_ID2 ). selectedIndex
$w ( txtFieldID2 ). value = ITEMS [ INDEX [ OUTPUT_FIELD2 ];
});

but now what is happening is there is no data from the databse being populated in the dropdown menus.

You forgot something → Read the post again!
create_UniqueDropdown1(ITEMS); —> 1x ?

Another failure → That part…

$w(DD_ID2).onChange(()=>{
   let INDEX = $w(DD_ID2).selectedIndex
   $w(txtFieldID2).value= ITEMS[INDEX[OUTPUT_FIELD2];
});

Goes to the → onReady-part (place it inside it). Take a look where you can find the same coding for → DD_ID1

That article confused me more than it helped me. Your posts were more helpful. I found the error in this code and corrected, i believe:

$w ( DD_ID2 ) . onChange ( () =>{
let INDEX = $w ( DD_ID2 ) . selectedIndex
$w ( txtFieldID2 ) . value = ITEMS [ INDEX] [ OUTPUT_FIELD2 ] ;
} );

so close, the 1st drop in works great and populates the 1st text box, the 2nd drop in works great but does not populate the 2nd text box. I checked all my ID’s and they all seem correct.

here is what i have:
import wixData from ‘wix-data’ ;

var DBFIELDS = [], ITEMS ;

//-------- USER-INTERFACE -------------------------------------------------
var txtFieldID1 = “#textBox1
var txtFieldID2 = “#textBox6

var DD_ID1 = “#dropdown1
var DD_ID2 = “#dropdown2

var DATABASE = “RegistrationAgeandSkill”
var DBLIMIT = 1000

var OUTPUT_FIELD1 = “ageValue”
var OUTPUT_FIELD2 = “skillValue”

DBFIELDS [ 0 ] = “age”
DBFIELDS [ 1 ] = “skill”
//-------- USER-INTERFACE -------------------------------------------------

$w . onReady ( async ()=>{
await wixData . query ( DATABASE )
. limit ( DBLIMIT ). find ()
. then ( results => {
ITEMS = results . items
create_UniqueDropdown 1 ( ITEMS );
create_UniqueDropdown2 ( ITEMS );
});

$w ( DD_ID1 ). onChange (()=>{
let INDEX = $w ( DD_ID1 ). selectedIndex
$w ( txtFieldID1 ). value = ITEMS [ INDEX ][ OUTPUT_FIELD1 ];
});
});

function create_UniqueDropdown1 ( items ) {
const uniqueTitles1 = getUniqueTitles1 ( items );
$w ( DD_ID1 ). options = buildOptions1 ( uniqueTitles1 );

function getUniqueTitles1 ( items ) {
const titlesOnly = items . map ( item => item [ DBFIELDS [ 0 ]]);
return [… new Set ( titlesOnly )];
}

function buildOptions1 ( uniqueList1 ) {
return uniqueList1 . map ( curr => {
return { label : curr , value : curr };
});
}
}
function create_UniqueDropdown2 ( items ) {
const uniqueTitles2 = getUniqueTitles2 ( items );
$w ( DD_ID2 ). options = buildOptions2 ( uniqueTitles2 );

function getUniqueTitles2 ( items ) {
const titlesOnly = items . map ( item => item [ DBFIELDS [ 1 ]]);
return [… new Set ( titlesOnly )];
}

function buildOptions2 ( uniqueList2 ) {
return uniqueList2 . map ( curr => {
return { label : curr , value : curr };
});
}
}
$w ( DD_ID2 ). onChange (()=>{
let INDEX = $w ( DD_ID2 ). selectedIndex
$w ( txtFieldID2 ). value = ITEMS [ INDEX ][ OUTPUT_FIELD2 ];
});

If you see, i added the code part to start all these actions.

Do i need to do something with the UniqueTitles in order to populate the value in that 2nd text box?

I know you said i’m on my own now…but i’m struggling here and so close to the end. Please help.

YES you are very close → but you should read better my advices :wink:

I told you to place this part here…

$w(DD_ID2).onChange(()=>{let INDEX =$w(DD_ID2).selectedIndex$w(txtFieldID2).value= ITEMS[INDEX][OUTPUT_FIELD2];});

INSIDE → $w.onReady()

The onReady-Part looks like this…

$w.onReady(()=>{
//—> put in CODE here (INSIDE onReady-part)
//—> put in CODE here (INSIDE onReady-part)
//—> put in CODE here (INSIDE onReady-part)
//—> put in CODE here (INSIDE onReady-part)
});

//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)
//—> put in CODE here (OUTSIDE onReady-part)

All you have had to do, is to place the second onChange-Event-handler directly under the already existing (first one).

And now → back to the ROOTS!

Normaly this forum is NOT a service-place where you can get code for FREE.

But since you are a novice, i want you to know, that it is not that easy as you perhaps expect to code a good working solution, even if you thinking that it should be an easy task.
I hope you saw now, how complicated it can be to code just something very basic & simple. Ok i am honest, this is already far away from basic, because here you have a more flexible code, which can variate/change quickly, without a lot of changements. You just add some code-lines and you get a further function.

How ever, this was just an example, of what can be done with JS.
There is much more potencial/ability, to improve this CODE and give it even much more complexity & some kind of → artificial intelligence🤓

If i would have known how complex this was i would have found a different route but had so much time invested that i wanted to see it to the end. i also learned a bunch and helps understand if i ever do this again the thought process behind mapping this all out. If i understand your direction correctly my last step is this:

$w.onReady(() =>{ $w ( DD_ID2 ). onChange (()=>{ let INDEX = $w ( DD_ID2 ). selectedIndex $w ( txtFieldID2 ). value = ITEMS [ INDEX ][ OUTPUT_FIELD2 ];});

your time is appreciated and as a non profit group it is a big help and will help our group and people. thank you Ninja!

So not to let you alone in the dark, i will provide you the END-RESULT, to show you what you have missed at the end. I also improved 1, or 2-code-parts → that means, the shown code will be different to your own.

import wixData from 'wix-data';

var DBFIELDS=[], DD_ID=[], OUTPUT_FIELD=[]; //<--- UPGRADED-VERSION !!!

//-------- USER-INTERFACE -------------------------------------------------
var txtFieldID1 = "#textBox1"
var txtFieldID2 = "#textBox6"

var DBLIMIT = 1000 
var DATABASE = "RegistrationAgeandSkill" 

//---[ Dropdown-Settings]---------------------
  DD_ID[0] = "#dropdown1"           //<--- UPGRADED-VERSION !!!
  DD_ID[1] = "#dropdown2"           //<--- UPGRADED-VERSION !!!
//---[ DB-INPUT-Field-Settings]--------------
  DBFIELDS[0] = "age"               //<--- UPGRADED-VERSION !!!
  DBFIELDS[1] = "skill"             //<--- UPGRADED-VERSION !!!
//---[ DB-OUTPUT-Field-Settings]--------------
  OUTPUT_FIELD[0] = "ageValue"      //<--- UPGRADED-VERSION !!!
  OUTPUT_FIELD[1] = "skillValue"    //<--- UPGRADED-VERSION !!!
//-------- USER-INTERFACE -------------------------------------------------
 
$w.onReady(async()=>{
   let ITEMS = await get_DBdata(DATABASE); 
   create_UniqueDropdown(ITEMS, DBFIELDS[0], DD_ID[0]); //<--- UPGRADED-VERSION !!!
   create_UniqueDropdown(ITEMS, DBFIELDS[1], DD_ID[1]); //<--- UPGRADED-VERSION !!!

  $w(DD_ID[0]).onChange(()=>{
     let INDEX = $w(DD_ID[0]).selectedIndex
     $w(txtFieldID1).value = ITEMS[INDEX][OUTPUT_FIELD[0]]; 
   }); 

   $w(DD_ID[1]).onChange(()=>{
     let INDEX = $w(DD_ID[1]).selectedIndex
     $w(txtFieldID2).value = ITEMS[INDEX][OUTPUT_FIELD[1]]; 
   }); 
}); 

function create_UniqueDropdown(items, dbfield, dropdown) { //<--- UPGRADED-VERSION !!!
  const uniqueTitles = getUniqueTitles(items);
  $w(dropdown).options = buildOptions(uniqueTitles); 
 
  function getUniqueTitles(items) {
    const titlesOnly = items.map(item => item[dbfield]); 
    return [...new Set(titlesOnly)];
  }

  function buildOptions(uniqueList) {
    return uniqueList.map(curr => {
      return {label:curr, value:curr};
    });
  }
}

function get_DBdata(DATABASE) { //<--- UPGRADED-VERSION !!!
  wixData.query(DATABASE)
  .limit(DBLIMIT).find()
  .then(results=> {
     let ITEMS = results.items 
     return (ITEMS)
  }); 
}

What has been changed?

In this UPGRADED-VERSION, you won’t have several —> “create_UniqueDropdown()” - functions.
You just have one function, but you can populate dropdowns as much as you want, with just THE ONE mentioned function, like …

create_UniqueDropdown(ITEMS, DBFIELDS[0], DD_ID[0]);
create_UniqueDropdown(ITEMS, DBFIELDS[1], DD_ID[1]);
//create_UniqueDropdown(ITEMS, DBFIELDS[2], DD_ID[2]);
//create_UniqueDropdown(ITEMS, DBFIELDS[3], DD_ID[3]);

I did not test it and i hope it works without any errors.

Hi Ninja. Sorry for the delay, had the kids all day. The dropdowns are working very well. Now i’m in the process of getting the data from the form to populate in a seperate collection so that i can export. Again, you have been a saint to work with and appreciate your help!!!

Also, when i get a second, i will try the new code!

I screamed in excitement when the dropdowns and text boxes worked!

@johnnystephens
Have fun :stuck_out_tongue_winking_eye::v:

Ninja. Hope you are having a good week. I am getting ready to launch this site and very excited. I could have not done this without you. I have a question that i can not figure out and researched and can not find the answer. I dont know if i’m not looking up the wrong keywords. Anyway, when i submit a practice form to see if the form is working, it does. However, when i go to view the data in the dataset there are 2 columns that show an error; the "AGE VALUE " and the “SKILL VALUE”. If you remember these 2 fields/dropdowns we coded, i believe this is why the error is generating. I went and looked at the code to see if there is a function that needs to be changed so that when the form is submitted the data is captured and moved to the dataset. I just can figure out which one. Any suggestions?

Seems to be a → TYPE-ERROR.

STRING and NUMBER are not the same.

STRING → “999”
NUMBER → 999

You will have to convert STRING to NUMBER on right place in your code.
Your Field-Type is → #Number
But you surely try to place → STRING into it.

Convert STRING → to → NUMBER

let myNumber = Number(myString)

Ninja. Hope you had a good Friday. I’ve done some research on this and even found some posts with your suggestions in other areas of the forum and have tried this a few times, not sure what i’m doing wrong, i dont believe i’m placing this code:

let myNumber = Number ( myString )

in the right location or changing the right information. Thoughts? Suggestions?
Johnny

You are already working on a totaly another issue, which has nothing to do with “dropdown-population” any more.

You should better open a new post, with your new issue.
Describe the issue as most detailed as possible, and also show related code-parts, which are involved.

Also not clear how looks like your form (custom form? / wix-form)?

will do. thank you and have a good weekend.