Discussion:
Hi, This is my second thread on the same “unanswered” question. I need someone to show me how to correctly “code” passing a number with a submit button into the CMS database. I have rewritten my code into a simpler and more organized manner to reduce confusion.
(1) I call a function called getApplicationID, It reads a number out of the CMS database. It increments the number by 3.
It writes that number into a session ID that can be retrieved across multiple web pages.
(2) I have an export function SubmitT4_click submit button that performs the following tasks:
a. It validates #firstName and #lastName
b. It creates an object (objTb) this object is nested into (wix_data_obj) and it is sent to the backend (SubmitT4.jsw)
c. (wix_data_obj) has elements:
#firstName,
#lastName,
uniID (a number) | This number UniID is not going into the CMS database
(but #firstName and #lastName are going into the CMS database when submit is
clicked)???
**How do I pass a number (uniID) when I click submit??? Code example please
*
Product:
Wix Studio Editor with Dev Mode enabled
What are you trying to achieve:
Taking the session number after i increment it and use that as a new application ID and upload that into the database as a primary ID key for linking all new applicant data across multiple web pages and CMS database tables.
Additional information:
site: www.PrivateTacticalArmory.com
Page: T4
Front-end Code
import wixData from 'wix-data';
import { session } from 'wix-storage-frontend';
import { process } from 'backend/submitT4.jsw';
//Variables
const collectionAppID = "AppID";
const appIDKey = "bc5e76e2-6b5e-4099-8392-9ede9be72d34";
let appNumberInc = 3;
var newAppID = 0;
function getApplicationID (collectionAppID, appIDKey, appNumberInc){
wixData.get(collectionAppID, appIDKey) //read base number from CMS
.then((item) => {
let newAppID = (item.appId + appNumberInc); //Increment by 3
session.setItem("AppKey", newAppID); //Set session key with incremented value
return newAppID
})
}
export function SubmitT4_click(event) {
//Validate and Send data to T4 DB
if ($w('#firstName').valid &&
$w('#lastName').valid) {
//Create an object
let objTb = {
//Token
//token: $w('#captcha1').token,
//nested wix-data obj
wix_data_obj: {
firstName: $w('#firstName').value,
lastName: $w('#lastName').value,
uniID: newAppID //Send number to CMS as part of the object
// uniID: uniID: session.getItem("AppKey") //Doesn't work
// uniID: newAppID //Send number to CMS as part of the object
// uniID: session.getItem("AppKey")
// uniId: session.getItem("AppKey").toString()
// uniID: myNumber //tried using let, const, var - nothing working?
}
};
//Send object to the backend & receive response
process(objTb)
.then((res) => {
if (res.success) {
$w('#dbSuccessText').show();
//wixLocation.to(`${wixLocation.url}`); //refresh page if sucess
}
});
} else {
$w('#dbErrorText').show();
}
}
$w.onReady(function () {
//at page load call function for new application ID
getApplicationID (collectionAppID, appIDKey, appNumberInc)
//Get session value using key
$w('#textNumber').text = session.getItem("AppKey"); //Text display
console.log(session.getItem("AppKey"));
});
Back-end Code (SubmitT4.jsw)
//import wixCaptchaBackend from 'wix-captcha-backend';
import wixData from 'wix-data';
export function processT1(objTb) {
// Authorize token and insert data
//return wixCaptchaBackend.authorize(objTb)
//.then(() => {
return wixData.insert("T4", objTb.wix_data_obj)
.then(() => ({ success: true }))
.catch((error) => ({ success: false }));
//})
//.catch((error) => ({ success: false }));
}
Screenshots
