Post Non-Input data to same row as Input data

I’m a beginner and developing a website that has a separate page for each of my sales people. On each page are User Input boxes for visitors to provide their info (first, last, and email address). They are connected to a the same dataset and are posted upon clicking a submit button that is also connected to the dataset.

The dataset collection also needs to indicate who’s page the visitor info was input from. I added an onClick event to the submit button and some code to add the non-input information (salesPersonName, salesPersonEmail). Unfortunately the only code I could come up with (below) posts the sales person’s name and email on a separate row and looks like this:

I can’t figure out how to write the code so they post on the same row and look like this:

Would greatly appreciate anyone who can show me how to fix my code so it posts on the same row. See link to my editor at bottom.

MY CODE:
import wixData from ‘wix-data’;
export function buttonSQUEEZEtoINFO_click(event, $w) {
let toInsert = {
“salesPersonName”: “Jill Hill”,
“salesPersonEmail”: “jh@yahoo.com”,
};
wixData.insert(“MASTER”, toInsert)
.then((results) => {
let item = results;
})
.catch((err) => {
let errorMsg = err;
});
$w(“#stripSQUEEZE”).collapse();
$w(“#stripINFO”).expand();
$w(“#stripBUY”).expand();
}

LINK TO EDITOR:
https://editor.wix.com/html/editor/web/renderer/edit/7890690e-8e76-4b76-8ff0-7d548ca4587a?metaSiteId=ff09e431-ad4c-4411-a601-346abbe1d413&editorSessionId=8AC77A97-9172-418E-9E10-E8FCB2EDC0FC&referralInfo=my-account

I think this will work better for you if you add a data hook for beforeInsert and set the sales people data there :

Dalia,
Thank you for directing me to beforeInsert(). I think it got me close. Option-1 Backend Data.js Code (below) posts on the same row, but I can’t get it to post the page specific information that is actually needed. See comments in italic. Option-2 Page Code doesn’t respond at all. Couldn’t find anything in the documentation that indicates how to make either of these work as needed, but it would be nice if they both did. Can you tell me how to fix Greatly appreciate it.

OPTION-1: BACKEND DATA.JS CODE
export function MASTER_beforeInsert(item, context) {
let hookContext = context;
item.title = item.title.toUpperCase(); //Works, but included for test purposes only.
item.salesPersonName = item.salesPersonName = “SPN”; //Posts, but included for test purposes only.
item.salesPersonEmail = item.salesPersonEmail = “#textSPE”.text;
//Doesn’t Post- “#textSPE” is a text box on the sales person’s page with their email address in it.
return item;
}

OPTION-2: PAGE CODE
export function MASTER_beforeInsert(item, context) {
let hookContext = context;
item.title = item.title.toUpperCase(); //Included for test purposes only. Doesn’t work
item.salesPersonName = item.salesPersonName = “Lisa Smith”; //Doesn’t post
item.salesPersonEmail = item.salesPersonEmail = “ls@gmail.com”; //Doesn’t post
return item;
}

Why are you setting the value twice:
item.salesPersonName = item.salesPersonName = “Lisa Smith”;
Why not :
item.salesPersonName = “Lisa Smith”;
?

Dalia,
Thanks for pointing that out. It’s not my intent to set the value twice, that’s just how I found it in some sample code and it actually only posted once when used that way in the backend Data.JS code. But, per your comment I took one of the ‘item.salesPersonName =’ out (see revised code below). The backend Data.JS code, line 1.4 still worked with that change, but Line 1.5 still does not and it’s what I really need. The Option-2 Page Code also still does not work, even with the changes.

· Why does 1.4 work and 1.5 does not?
· Why doesn’t any of the Option-2 Page Code work?

Really appreciate you helping me figure this out. It’s critical to my website.

OPTION-1: BACKEND DATA.JS CODE (Revised)
1.1. export function MASTER_beforeInsert(item, context) {
1.2. let hookContext = context;
1.3. item.customerLast = item.customerLast.toUpperCase(); //WORKS- Incl. for test purposes only.
1.4. item.salesPersonName = “SPN”; //POSTS- Incl. for test purposes only.
1.5. item.salesPersonEmail = “#textSPE”.text; //Does NOT Post
1.6. //“#textSPE” is a text /paragraph box in a strip on the sales person’s page.
1.7. return item;
1.8. }

OPTION-2: PAGE CODE (Revised)
2.1. export function MASTER_beforeInsert(item, context) {
2.2. let hookContext = context;
2.3. item.customerLast = item.customerLast.toUpperCase(); // Doesn’t work. Incl. for test only.
2.4. item.salesPersonName = “Lisa Smith”; //Does NOT post
2.5. item.salesPersonEmail = "ls@gmail.com "; // Does NOT post
2.6. return item;
2.7. }

If you want the value of a text box you need to call $w(" #textSPE ").text

Dalia,
Thank you, but now I’m confused. You originally suggested that I use an afterInsert() hook. The Wix Code excerpt below says that hooks reside in the Backend.


Contrary to that, your last comment indicated that I need to use $w to call the value of a text box. The Wix Code excerpt below says that “$w can only be used in front-end code.”

You’re right, I got confused.
data.js is backend code and cannot access your text fields.
If the extra data you need is in a text box why don’t you just connect the test box to the db, why are you trying to do it manually?

Dalia,
Thank you and great question. The core of my answer is in two parts.

  1. Because there are some valuable benefits to this method, I have tried connecting the text boxes directly to the dataset numerous times, but they just would not post. I double checked that a) they were connected to the one and only dataset that I have, b) they were connected to the correct field in the dataset, and c) the field types are appropriate (text to a Text type field). All of the input boxes post nicely every time the connected submit button was clicked, but the connected text boxes do not. Because Wix gives the option of connecting text boxes to a dataset, there has to be some little thing I’m doing wrong that is preventing them from posting. I have to say, trying to make this method work has been a bit frustrating.

  2. Posting manually via Page code (vs. Site code) would also have some valuable benefits. One benefit is not having to have text boxes on the page for the sole purpose of posting typical page specific (non-input) information along with input information. It would be really great to be able to do this as well!

There are no Forum questions nor Wix documentation that resolve these two issues (that I could find after hours of searching). Is there any chance you could try to get these 2 options to work on one of your test sites (or I can give you a link to one) and then show me and the rest of the world how you did it? I think you would be a hero to a lot of people if you could.

Hi
I copied your site and managed to achive it by adding this code to the ‘kulda, Vanessa’ page :
import wixData from ‘wix-data’;
$w.onReady( function () {
$w(“#dataset1”).onBeforeSave(() => {
$w(“#dataset1”).setFieldValue(‘salesPersonName’, ‘Dalia’);
$w(“#dataset1”).setFieldValue(‘salesPersonEmail’, ‘Dalia@wix.com’);
});
});

This sets the 2 fields you wanted before saving the data. Because it’s on the page you can also read the value from a text box if you want.

Dalia,
WOW! You are my hero. Both work great and are going to be a huge help to my site. Here is my full working code and a couple of notes.

Post Non-User Input Data: Manually with Code
import wixData from ‘wix-data’;
$w.onReady(function () {
$w(“#datasetMASTER”).onBeforeSave(() => {
$w(“#datasetMASTER”).setFieldValue(‘salesPersonName’, ‘Lisa Smith’);
$w(“#datasetMASTER”).setFieldValue(‘salesPersonEmail’, ‘ls@gmail.com’);
});
});

Post Non-User Input Data: From textBox
import wixData from ‘wix-data’;
$w.onReady(function () {
$w(“#datasetMASTER”).onBeforeSave(() => {
$w(“#datasetMASTER”).setFieldValue(‘salesPersonName’, $w(“#textSPN”).text); //textSPN is a textBox
$w(“#datasetMASTER”).setFieldValue(‘salesPersonEmail’, $w(“#textSPE”).text);
});
});

NOTES:

  1. I found that if either of these do not work at first, entering the $w using the completion panel by pressing Ctrl + Space fixed the problem.
  2. Trying to do both in the same onReady function (as shown below) only worked partially for me. The ‘manually with code’ posted, but ‘from textbox’ did not. Any thoughts on why?

import wixData from ‘wix-data’;
$w.onReady(function () {
$w(“#datasetMASTER”).onBeforeSave(() => {
$w(“#datasetMASTER”).setFieldValue(‘salesPersonName’, $w(“#textSPN”).text); //From textBox
$w(“#datasetMASTER”).setFieldValue(‘salesPersonEmail’, ‘Dalia@Wix.com’); //Manually with code
});
});