Prefill textbox using WixCode

Hi All,

I have a input form which populates a dataset on a page. However i need to prefill 3 of the textboxes with data from a seperate dataset.

For this reason, i am unable to link it to the ‘read’ dataset as i will then not be able to link it to the submit form.

There is a set filter on the dataset that finds ‘logged in user’, users only have one entry in the prefilling dataset and therefore this resulting entry will be the one i need to prefill my textboxes with.

Any ideas anyone, been stuck on this a while :frowning:

Hi Tim,

To prefill the value you will need to use wixQuery and query the collection to find the relevant value.

wixUsers allows you to check if a user is currently login, then replace the value of the input fields:
https://www.wix.com/code/reference/wix-users.html#currentUser

For example:

import wixUsers from 'wix-users';

let user = wixUsers.currentUser;
let isLoggedIn = user.loggedIn; 

$w.onReady(function () {
 if (isLoggedIn) {
        //get age and name from collection with wixQuery, then:
        $w('#nameInput').value = "John Johnson"
        $w('#ageInput').value = 2
    }
});

Hi Ido,

Thanks for your responce, i get the layout you have used however i’m struggling to input my query into this. The help article is as below but how would i build the ‘loggedinuser’ into the search term and then reference the found result field to the textbox.

import wixData from ‘wix-data’;

// …

wixData.query(“myCollection”)
.find()
.then( (results) => {
let firstItem = results.items[0]; //see item below
} )
.catch( (err) => {
let errorMsg = err;
} );

/* firstItem is:
*

  • {
  • “_id”: “00001”,
  • “_owner”: “ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb”,
  • “_createdDate”: “2017-05-24T12:33:18.938Z”,
  • “_updatedDate”: “2017-05-24T12:33:18.938Z”,
  • “title”: “Mr.”,
  • “first_name”: “John”,
  • “last_name”: “Doe”
  • }
    */

Tim

Hi Tim,

At the end is should look similar to this:

import wixUsers from 'wix-users';
import wixData from 'wix-data';

let user = wixUsers.currentUser;
let isLoggedIn = user.loggedIn;

$w.onReady(function () {
 if (isLoggedIn) {
        wixData.query("myCollection")
            .eq("someField", "somevalue")
            .find()
            .then((results) => {
                let firstItem = results.items[0]; //see item below
                $w('#nameInput').value = firstItem.name;
                $w('#ageInput').value = firstItem.age;
            })
            .catch((err) => {
 let errorMsg = err;
            });
    }
});

Hi Ido,

I think i understand, however it’s still not doing what i’d like it to do. There will only ever be one result for the logged in user in the dataset i am searching. Heres my code:

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;

let user = wixUsers.currentUser;
let isLoggedIn = user.loggedIn;

$w.onReady(function () {
if (isLoggedIn) {
wixData.query(“#dataset2”)
.eq(“owner”, user)
.find()
.then((results) => {
let firstItem = results.items[0]; //see item below
$w(‘#input2’).value = firstItem.businessName;
$w(‘#input3’).value = firstItem.location;
$w(‘#input4’).value = firstItem.website;
});
}
});

Try this:

import wixUsers from 'wix-users';
import wixData from 'wix-data';

let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;


$w.onReady(function () {
if (isLoggedIn) {
       wixData.query("#dataset2")
           .eq("owner", userId)
           .find()
           .then((results) => {
               let firstItem = results.items[0]; //see item below
               $w('#input2').value = firstItem.businessName;
               $w('#input3').value = firstItem.location;
               $w('#input4').value = firstItem.website;
           });
   }
});
1 Like

Hi Ido,

I managed to find the issue, i was attempting to query the dataset as apose to the collection! Work’s perfectly now. Thanks for your help.

Tim

Just to add to this whilst on the subject of filtering by owner.

For a difference page i need do the same thing however, if there is an entry in the collection for the logged in user. then i need to change a button’s text and link. If they dont have an entry, change it to something else and a different hyperlink.

It’s a kind of register form, so if they havent registered, it asks them to do so, if they have, it diverts to ‘Manage’