Submit form data to two collections with same ID

Hi Guys,

New Wix user here, started using Dev mode a few days ago to make my own custom form on a slideshow. It submits data to 2 different data collections.
Data collection 1 = Contact Information, such as name, phone, address etc.
Data collection 2 = Health information, such as previous injuries.

I cannot have any personal identifiable information along with health information located in the same data collection. Therefore, I have separated them into these two data collections.

My question is, when the user submits the form and the form data goes into the two different data collections, how can I later link the two using a reference field ?

I have briefly gone over the features of a reference field, and to my understanding I would have to manually link the Contact information to the Health information… which is unsuitable as I would be guessing for each record in the data collection.

My thoughts would be to have a column in both data collections called “linkID” which would be the same in both collections. Therefore they would be linked using the linkID.

Do I create an invisible field with an automatically generated linkID that can be submitted to both data collections??
Or can I add some code to the submit button which automatically generates a linkID field ??

Thanks in advance.

First of all, could you provide your code for the submission …

This is the only code I am using thus far. It’s just to validate the fields have data before allowing the ‘next’ button to show the next slide.

masterpage.js:


$w.onReady(function () {
 // Write your code here
    $w('#ButtonContinue').disable()
});

page code:

export function FName_change_1(event) {
 // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
 // Add your code for this event here: 
 if ((($w('#FName').value) === "" ) ||
    (($w('#LName').value) === "" ) ||
    (($w('#EAddress').value) === "" ) ||
    (($w('#Phone').value) === "" ) ||
    (($w('#DOB').value) === "" ) ||
    (($w('#Age').value) === "" ) ||
    (($w('#Address').value) === "" ) ||
    (($w('#Occupation').value) === ""  )){
        $w('#ButtonContinue').disable()
    } else {
        $w('#ButtonContinue').enable()
    }
}

I have not setup the submit button code yet, I have seen it being done in other posts on the forum, so will incorporate that after i have finished developing the form.

@pauljdacre Sorry, I thought it was (another) post…
Sorry !!!

Here, First you need a insert function

let toInsert = {                      //modify as your need
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

wixData.insert("Database1", toInsert)   
	.then( (results) => { 
	//here is the main part
	let id = results[0]._id;
	
	let toInsert = {                      //modify as your need
  "linkId":        id,    
  "first_name":   "John",
  "last_name":    "Doe"
};

wixData.insert("Database2", toInsert)   
	.then( (results) => { 
	
	});
		
	})

For all these, you need 2 database (which you already have)
In the 2nd database create a new field called “linkId”
That’s all !!!

Hi Ajit,

Thanks for your response. As I’m not familiar with this coding language you might need to help me a bit further. I think your solution means I need to submit all my form data with this button?

I think to make it simple and easy for me, can we try the following instead?

-I will make an invisible text field on the form called “linkIDText”
-When the page loads, can it automatically generate a random ID and fill this ID into the “linkIDText” field.
-Then, we user clicks submit on the submit button… all of the form data will go to the database like it normally does, but also send “linkIDText” field to Both database.

I hope you understand my requirements, let me know if there is an easier way.

Hi Ajit,

I just thought of a way which might work for my requirements.

The form is on a slideshow.
The first slide contains firstname, lastname, etc… which needs to go into Database1.
The other slides contain health information… which needs to go into Database2.

Can I have a button on Slide1 which submits firstname, lastname etc… and then loads Slide2. When Slide2 loads, it will read/reference the ID from the submitted data from Slide1, and then populate that into a text field on Slide2 ?

That way i can submit the ID with the same number into database2 aswell.

If you want to generate a random number for the field “fieldId”
then you can use the Math.random() →

 var number = Math.floor((Math.random() * 10) + 1);
 console.log(number) //random number between 1 and 10

Hi Paul,

I stumbled upon this thread that’s looks close enough to where I’m stuck currently! In case this reaches you, pl help out.

a) Were you able to deliver LinkID to both the collections?

b) Were you next able to use ‘reference fields’ to display data somewhere?

I’d indeed need some help from you in case you were able to sort.

You normaly do not need an extra “linkedID-Field” because you have already fields like → “_id” and “_owner”.