Hi everyone,
I am looking to create a tool which allows users of my site to generate a letter with their personal details, taken from a form.
Essentially, I want to create something where users provide their name, age and birthday date, and this is interpolated into this text: “Hello, I am [name], I turned [age] on the [date]” and displayed onscreen.
Is this possible? If so, how!? I have a little coding experience (Ruby), and a basic understanding of databases.
Thanks,
Harry
1 Like
I am assuming you have already passed their form data successfully to a database…
What you want to do next is set up a dynamic page. Read the users form data from the database and display it on the dynamic page.
Hi Mike, I’m unsure how to actually get it to present the data in the database. I’ve created a dynamic page, but don’t knkw where to go from here.
@nounpaidtrialshifts
What we can do is write the static letter as a string in wix code, we will then add the variable words from the database to static letter in wix code. Finally we will display the completed letter as text on the screen.
What is your database name ?
What are the field IDs in your database that your storing the varable words in ?
HI Mike,
Thanks so much for your help. The database is called Trial_Shifts and the values are “name”, “hours” and “date” (date is stored as a string [formatted DD/MM/YYYY] rather than a date as I didn’t see an option to only show date, Wix allowed only for date and time).
I hope for the letter to appear like this:
"To whom it may concern,
I am writing to inform you that I undertook an unpaid trial shift lasting [hours] hours on [date].
Thanks,
[name]"
Any assistance with how and what I must code to achieve this would be much appreciated.
Thanks!
place a dataset on your dynamic page and connect it to Trial_Shifts.
place some text on screen.
set the text with the code below:
import wixData from ‘wix-data’;
$w.onReady( function () {
$w("#dataset1").onReady(() => {
const name = $w(“#dataset1”).getCurrentItem().name;
const hours = $w(“#dataset1”).getCurrentItem().hours;
const date = $w(“#dataset1”).getCurrentItem().date;
let completeLetter = (“replace this sample text " + (hours) + " replace this sample text " + (date) + " replace this sample text " + (name) + " replace this sample text”);
//place some text on screen then set the text on screen with code below
$w("#text1").text = completeLetter;
})
})
Sorry to be a pain.
I copied your code into the console. I created some text (I wrote “letter”) and made your code@
“$w(” #l etter").text = completeLetter;"
it says dataset1 is not a valid selector. I changed “dataset1” to the apparent name of my dataset, “Trial_Shifts Item”, but the message persists.
When the page is previewed, the console says “TypeError: $w(…).onReady is not a function”, and the “letter” text remains unchanged.
Any ideas?
to get the dataset ID, right click the dataset and choose view properties.
Thank you!
I’ve changed the text ID and the dataset ID to the correct ones. Now this error is appearing! "An error occurred in one of datasetReady callbacks TypeError: $w(…).getCurrentItem is not a function "
you get that error when you have the wrong dataset ID written in the code.
change the ID of your dataset to " dataset1 "
@mikemoynihan99 Typo found! The text is now changing, but each variable is coming up as “undefined”.
@nounpaidtrialshifts
are you sure your database field ID are correct (name, hours, date) ?
do you have data stored in the database under these field IDs ?
This is the database:
This is the page in previous mode:
Here’s the code:
can you delete all the empty rows, i suspect you may be reading from one of the other rows
Completely cleared the database so only one row exists. The problem persists!
@nounpaidtrialshifts
i see the problem, you have not copied my code correctly. Database field IDs always start with a lower case character…
@mikemoynihan99 I apologise for being such a nightmare! It’s working, thank goodness. I feel like I ought to credit you