[RESOLVED] Value not setting properly

Hello all. I’m attempting to set the value of an input element on load for a form by using the .value method but it seems to be randomly working.

I pull data from the private members database using a query and store it in a variable (fN, lN, eM) and then set the value of the input to the variable (all in the same block)

$w(‘#input5’).value = fN

This doesn’t work. However when I changed just the above line to:
$w(‘#input5’).value = item[0].firstName
Not only did it fix the first line, but also the subsequent lines which were still referencing the variables.

To recap:
$w(‘#input5’).value = fN
$w(‘#input6’).value = lN
$w(‘#input8’).value = eM
didn’t work, but:
$w(‘#input5’).value = item[0].firstName
$w(‘#input6’).value = lN
$w(‘#input8’).value = eM
worked just fine. In fact, when I changed the others to pull directly from the query results the third line stopped working.

Any idea what’s happening here so I can avoid it all costs?

Make sure that you are using the correct field names for the collection as email is emails.
https://support.wix.com/en/article/corvid-wix-members-privatemembersdata-collection-fields

Also, note that when calling set field values on a read-only dataset it causes an error, of which the PrivateMembersData collection is.

Plus, have a read of this previous forum post:
https://www.wix.com/corvid/forum/community-discussion/pre-fill-form-fields-with-values-from-previous-record

If the page is just for members only, then in theory you could just add the collection to your page and then have elements on that page that are connected to the appropriate fields in your collection, so that it only displays the users own info.

Or you can try this code:

// Auto-Fill Form //
$w.onReady(function () {
       $w("#input1").value = "Daniel" // From database use: $w('#dynamicDataset').getCurrentItem().item //
       $w("#input2").value = "Philips"
       $w("#input3").value = "daniel.philips@gmail.com"
       $w("#input4").value = "+1 123 123 1234"
});
​
// Submit Auto-Filled Items //
​
export function Submit_click(event, $w) {
        $w('#dataset1').setFieldValue('fname', $w('#input1').value);
        $w('#dataset1').setFieldValue('lname', $w('#input2').value);
        $w('#dataset1').setFieldValue('email', $w('#input3').value);
        $w('#dataset1').setFieldValue('phone', $w('#input4').value);
} 

In this example, we hard coded the first name, last name, email and phone numbers.

You can either

  1. Change the auto-populated values (if your values are static)

  2. Retrieve the values from a dataset. For example, $w(“#dataset1”).getCurrentItem().fieldName;

Thanks for the quick reply.
For the field name I’m actually referencing the user’s login email and have the value set to loginEmail. I have all the values print to the console after each variable is set just to make sure that they are pulling the correct information.
When you say “dataset” are you referring to both datasets and databases? I try to avoid adding datasets to forms that need to auto-fill from one database and write to another as it seems to give me problems.
I’ve read the above thread and it definitely helped me getting started.
All in all, I’ve found a workaround for the problem but I’m trying to see where the error lies so as to avoid it as it took me a number of days to find a working solution.

Thanks again for your help.