PLEASE HELPP!! Connecting Dataset in Same Row (Resolved)

Hi,

I have created few pages where user’s need to input their data. But I don’t want to use dynamic pages. I have successfully created all the pages needed and connected the pages via button. I also managed to bring the data to the dataset. But I’m stuck here because the data I collected in Collection is not at the same row. I have attached a image below :

So I want the data to be in the same row for the same person. I know this can be done with dynamic page but I don’t want to use dynamic page.

Please help me through this, I’m on the verge to complete the website. Thank you!

If I may add all of it is a subpage. Thank you! Please help ou

Please help me I’m still stuck in this. Thank you!!

More details are needed. Add screenshots, and post your code.

Hi,

I haven’t created a code yet. Because I am unclear with what code to use. But as you can see from the below pic starting from “Database” to “7” is connected to the same dataset . Each page has a submit button which allows them to store the data inputted by user to the dataset collection called as “Customer_Details”.

But when I click submit in all the pages. The data stores in the collection as intended. But when it does, it saves under a different row for different pages. Example, if page “database” saves under Row 1 , and Page “1” saves under Row 2 and so on. How do I make it store under the same row ? That is what I would like to do.

Thank you for your time.

@testsalesmimota
What are pages 1-7? different pages of the same form? How do navigate between them? what’s the flow?

@jonatandor35

No, page “database” to page “7” contains different form.

Database - Details of customer like name and email
1- Monthly payment i.e : three months payment
2 - Average Saving
3 - Monthly interest
4 - Discount provided by us
5 - Location i.e Address
6 - Thank you
7 - Contact number and set appointment

But all this forms is compulsory for the users to go through in term to set appointment at the end or last page which is Page “7”.

But the user’s are allowed to input the data in page “database”, page “1”, page “7”. So only this details are collected. So, I don’t want them to overlap each other in different rows and store it in the same row. And all the information is crucial for us to collect in term to check their credit and contact them back.

All the page has a “Submit” button to link to next page.

The flow is as below :

Database > 1 > 2 > 3 > 4 > 5 > 6 > 7 > Home

@testsalesmimota so have a userId field in your collection and you can try using the following code on each of the pages:

import wixData from 'wix-data';
impot wixUsers from 'wix-users';
let userId = wixUsers.currentUser.id;
let userObj = {};
let inputFields = ["name", "email/*etc.. use the property names of the input elements. Make them the same as the field keys in your database, it'll make it shorter*/];
$w.onReady(() => {
    $w("#submitButton").disable();
    wixData.query("MyCollectionName")
    .eq("userId", userId)
    .find()
    .then(r => {
        r.items.length > 0 ? userObj = r.items[0] : userObj.userId = userId;
        inputFields.forEach(e => {
            $w(`#${e}').onChange(event => {
            if(inputFields.every(e => $w(`#${e}`).valid)){
                inputFields.forEach(e => userObj[e] = $w(`#${e}').value);
                $w("#submitButton").enable();
                })
            } else {
            $w("#submitButton").disable();
            //show message to fill in all fields
            }
        })
    })
$w("#submitButton").onClick(event => {
    wixData.save("MyCollectionName", userObj)
    .then(() => {
        console.log("done");
        //show success message
    }).catch(err => console.log(err));
})
})

@jonatandor35 Hi sorry for the late reply tho.

I’ve tried it out, still facing few issues. It is not uploading in the collection. Now I want to start from scratch for the user input part. As I said previously, there is 8 pages and all the pages should be connected under the collection called “Customer_Detail”. For the first step, sorry for asking how should I include user id and make sure all 8 pages are connect to it. Secondly even all the 8 pages are connected there is only few fields that I want the database to collect. All the pages has submit button to go the next page. Please give a thorough explanation and help with this.

Thanks once again and sorry for troubling you a lot.

@testsalesmimota

If you are struggling with the great code sample provided to you by J. D. already, then have a look at using the Wix Corvid multistage form that you can open up in your own Wix Editor and see how the slideshow works, plus all the needed code will already be on the page for you.
https://www.wix.com/corvid/example/multistage-form

Also, when you mention your submit button, are you using the Wix submit button from the Wix Editor as shown on this support page?
https://support.wix.com/en/article/adding-a-submit-button-to-your-form

As if you are and you don’t use dynamic pages, then that submit button will create a new entry in your dataset on each and every page before it then takes the user onto the next page, hence why you are getting the multiple rows of user inputs in your dataset.

If you are then you need to remove it and to use a normal button instead as J. D.'s code does the save for you with the ‘wixData.save(“MyCollectionName”, userObj)’ line of code.

If you are still struggling, then please note that this forum is only here to help and guide you towards your finished code for your site, it is not here for a way to get somebody to write your whole code for you.

If you need that option then you should be looking at going to Wix Arena and finding a Wix Expert that can do the job for you.
https://www.wix.com/arena/web-designers/1

@givemeawhisky Hi,
The multi form isn’t working really well for me as well. I tried JD’s code but something in it is missing.

import wixData from 'wix-data';
import wixUsers from 'wix-users';
let userId = wixUsers.currentUser.id;
let userObj = {};
let inputFields = ["title","emailaddress"];
$w.onReady(() => {
$w("#submitButton").disable();
wixData.query("Customer_Detail")
.eq("userId", userId)
.find()
.then(r => {
r.items.length > 0 ? userObj = r.items[0] : userObj.userId = userId;
inputFields.forEach(e => {
$w('#title'),$w('#emailaddress').onChange(event => {
if(inputFields.every(e => $w('#title').valid),$w('#emailaddress').valid){
inputFields.forEach(e => userObj[e] = $w('#title').value,$w('#emailaddress').value);
$w("#submitButton").enable();
]) // the closing bracket which is ']' is coming as error. 
} else {
$w("#submitButton").disable();
//Please fill up the details
}
})
})
$w("#submitButton").onClick(event => {
wixData.save("Customer_Detail", userObj)
.then(() => {
console.log("done");
}).catch(err => console.log(err));
})
})

Can you tell what mistake I made in this. Thank you and sorry for the trouble

Look at the code example from J. D. and you will soon see your own mistake…

J. D. Code…

$w("#submitButton").enable();
})
} else {

Your code…

$w("#submitButton").enable();
]) 
} else {

I will give you a clue… You use [ and ] for arrays like this line…

let inputFields = ["title","emailaddress"];

Hi,

I tried as you told but the result is coming as unexpected token for the air quotes "

let inputFields = ["#title" [and] "#emailaddress"];

@testsalesmimota
No you did not have to change that line, put it back as how it was in the example.

let inputFields = ["title","emailaddress"];

All you needed to do is to change that one error in your code.

It should be like this - }) - that is a } and a )

You have it like this - ]) - you have used ] and )

It is still coming out an error.

As you have not used J. D.'s code example on your page and instead trying to get it to work yourself, I have tested your code and it doesn’t work as you needed to modify it yourself.

I created a blank website with two (2) pages with #title and #email user inputs on the first page and #lfirstName and #lastName on the second page.

I used a dataset called the same as you have used as Customer_Detail, with the user inputs named exactly the same as the field keys from the dataset fields.

For both pages you need the user inputs connected to your dataset through the dataset link on each user input element with the dataset on each page.

For the button you do not connect it to the dataset through the dataset link on the button element.
On the first page in your code, you need to add a link to the second page, second page add a link to third page and so on with the last page having no link on it at all

The only drawback with you modifying J. D.'s code, is that as you are using the same onChange functions that work with his code. However, for your code the user has to click off of the user input box for the submit button to be enabled, so basically once they are done with the user inputs they need to click on the page for the code to register that the user input value has changed

To learn more about this, then see the Wix API reference for onChange.
https://www.wix.com/corvid/reference/$w.ValueMixin.html#onChange

So, the code that you need is as follows.

First page:

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

let userId = wixUsers.currentUser.id;
let userObj = {};
let inputFields = ["title","email"];

$w.onReady(() => {
$w("#submitButton").disable();
wixData.query("Customer_Detail")
.eq("userId", userId)
.find()
.then(r => {
r.items.length > 0 ? userObj = r.items[0] : userObj.userId = userId;
inputFields.forEach(e => {
$w('#title'),$w('#email').onChange(event => {
if(inputFields.every(e => $w('#title').valid),$w('#email').valid){
inputFields.forEach(e => userObj[e] = $w('#title').value,$w('#email').value);
$w("#submitButton").enable();
} else {
$w("#submitButton").disable();
//Please fill up the details
}
})
})
$w("#submitButton").onClick(event => {
wixData.save("Customer_Detail", userObj)
.then(() => {
console.log("done");
wixLocation.to(`/blank`);
}).catch(err => console.log(err));
})
})
})

Second page (last page for my example):

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

let userId = wixUsers.currentUser.id;
let userObj = {};
let inputFields = ["firstName","lastName"];

$w.onReady(() => {
$w("#submitButton").disable();
wixData.query("Customer_Detail")
.eq("userId", userId)
.find()
.then(r => {
r.items.length > 0 ? userObj = r.items[0] : userObj.userId = userId;
inputFields.forEach(e => {
$w('#firstName'),$w('#lastName').onChange(event => {
if(inputFields.every(e => $w('#firstName').valid),$w('#lastName').valid){
inputFields.forEach(e => userObj[e] = $w('#firstName').value,$w('#lastName').value);
$w("#submitButton").enable();
} else {
$w("#submitButton").disable();
//Please fill up the details
}
})
})
$w("#submitButton").onClick(event => {
wixData.save("Customer_Detail", userObj)
.then(() => {
console.log("done");
}).catch(err => console.log(err));
})
})
})

It all works when testing in preview and keeps the users data all to one row.

However, as you are using the Wix Users API you should be testing your site on a fully published live site and not through the preview.
https://www.wix.com/corvid/reference/wix-users.html
Note

  • The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.

  • The APIs in wix-users can only be used once the page has loaded. Therefore, you must use them in code that is contained in or is called from the onReady() event handler or any element event handler.

@givemeawhisky Hi, I worked like a charm. I applied some modifications and the data is all in a row but one tiny issue is that there is repetition of data. For an example, in page 1 there is “name” and “email address” but once clicked the button the dataset is saving the same data.

Let say, the user’s name is Joe and the email address is joe@gmail.com. But once i click submit, the dataset field of name and email address saves only “Joe” in both spaces and not “joe@gmail.com” at all.

^ This one is on preview

But once I publish my site, I could click Next but it is not moving on to the next page. It worked in preview but not in published.

import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
let userId = wixUsers.currentUser.id;
let userObj = {};
let inputFields = ["avgMonthlyBill"];
$w.onReady(() => {
$w("#submitButton").disable();
wixData.query("Customer_Detail")
.eq("userId", userId)
.find()
.then(r => {
r.items.length > 0 ? userObj = r.items[0] : userObj.userId = userId;
inputFields.forEach(e => {
$w('#Calculate1').onClick(event => {
if(inputFields.every(he => $w('#avgMonthlyBill').valid)){
inputFields.forEach(he => userObj[e] = $w('#avgMonthlyBill').value);
$w("#submitButton").enable();
} else {
$w("#submitButton").disable();
//Please fill up the details
}
});
});
$w("#submitButton").onClick(event => {
wixData.save("Customer_Detail", userObj)
.then(() => {
console.log("done");
wixLocation.to('/2'); // "2" is my page name
}).catch(err => console.log(err));
});
});
});

@testsalesmimota

With the link, make sure that your code is correct.

Correct code

wixLocation.to(`/blank`);

Your code

wixLocation.to('/2');

Correct code for you

wixLocation.to(`/2`);

You see where you went wrong…

@givemeawhisky

Hi, I’ve tried it but still the same. Same result hm. Don’t know what went wrong.

Page 1 :

import {session} from 'wix-storage';
$w.onReady(() => {
$w('#submitButton').onClick(event => {
session.setItem('test', $w('#title').value);
})
})
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
let userId = wixUsers.currentUser.id;
let userObj = {};
let inputFields = ["title","email"];
$w.onReady(() => {
$w("#submitButton").disable();
wixData.query("customers")
.eq("userId", userId)
.find()
.then(r => {
r.items.length > 0 ? userObj = r.items[0] : userObj.userId = userId;
inputFields.forEach(e => {
$w('#title'),$w('#email').onChange(event => {
if(inputFields.every(he => $w('#title').valid),$w('#email').valid){
inputFields.forEach(he => userObj[e] = $w('#title').value,$w('#email').value);
$w("#submitButton").enable();
} else {
$w("#submitButton").disable();
//Please fill up the details
}
});
});
$w("#submitButton").onClick(event => {
wixData.save("customers", userObj)
.then(() => {
console.log("done");
wixLocation.to(`/1`);
}).catch(err => console.log(err));
});
});
});

Page 2

$w.onReady(function () {
$w('#Calculate1').onClick(function () {
console.log("button has been pressed")
let first = $w('#month1').value;
let second = $w('#month2').value;
let third = $w('#month3').value;
$w('#avgMonthlyBill').value = ((+first + +second + +third) / 3)
console.log("calculation finished")
})
})
import {session} from 'wix-storage';
$w.onReady(() => {
$w('#submitButton').onClick(event => {
session.setItem('value', $w('#avgMonthlyBill').value);
})
})
$w.onReady(() => {
$w("#Calculate1").onClick(event => {
setTimeout(() => {
if (!isNaN($w("#avgMonthlyBill").value)){
$w("#avgMonthlyBill").value = parseFloat($w("#avgMonthlyBill").value).toFixed(2);
}
}, 2)
})
})
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
let userId = wixUsers.currentUser.id;
let userObj = {};
let inputFields = ["avgMonthlyBill"];
$w.onReady(() => {
$w("#submitButton").disable();
wixData.query("customers")
.eq("userId", userId)
.find()
.then(r => {
r.items.length > 0 ? userObj = r.items[0] : userObj.userId = userId;
inputFields.forEach(e => {
$w('#Calculate1').onClick(event => {
if(inputFields.every(he => $w('#avgMonthlyBill').valid)){
inputFields.forEach(he => userObj[e] = $w('#avgMonthlyBill').value);
$w("#submitButton").enable();
} else {
$w("#submitButton").disable();
//Please fill up the details
}
});
});
$w("#submitButton").onClick(event => {
wixData.save("customers", userObj)
.then(() => {
console.log("done");
wixLocation.to(`/2`);
}).catch(err => console.log(err));
});
});
});

Its repeats for all the pages. The “Next” button enables but not moving on to another page.