Integrate calculated data in database

Hello everyone!

I created a form so visitors can leave testimonials on my website that are then displayed in a repeater on my home page to show the 3 most recent ones. And here’s my issue:
Not only do I display the testimonial itself, but also the name and last name. But having two different fields in my form (and thus database), I had to create two text boxes in my repeater, one for the name and the other for the surname and first thought: I’m going to align the first text on the right and the other on the left. However, by doing so, the whole [name-last name] isn’t centered in the repeater unless both are of equal length (if the name is too long, the whole is too much on the left and vice versa if it’s the last name that’s too long).

To change that, I thought I could create a “fused” field in my database that would have had name+lastname as value, but discovered such fusion it is currently impossible on Wix unlike Excel.

So then, to counter the issue, of course, I thought of the simplest solutions like centering the first and last names by putting them one on top of the other or having only one field in my form for both first and last name, but I obviously found them much less aesthetic (the design I wish for isn’t optimal for the first solution [leaves a void under the name if the person doesn’t insert any last name] and the second one seems too unusual for visitors and less “pretty”)…

I ended up wondering if the solution couldn’t actually be coded. Through coding, I thought of creating a third field (invisible for the visitors) which would add the first and last name fields at the time of the input by the visitor and whose value would then be inserted in my name+lastname field in my database. But obviously, I didn’t manage to do that…
For now, I have a code that looks like this:

let valeurData1 = “0”
let valeurData2 = “0”

$w.onReady(function () {});
export function prenom_input(event) {
valeurData1 = $w(‘#prenom’).value;
}
export function nom_input(event) {
valeurData2 = $w(‘#nom’).value;
$w(‘#result’).text = valeurData1 + " " + valeurData2;
}
export function temoignages_ready() {
$w(‘#temoignages’).getCurrentItem().prenomNom = $w(‘#result’).text;
$w(‘#temoignages’).save();
}

I know that this code is wrong and will never work, but is there a way to do what I’d like to do with a similar code (or just any code) or is it simply impossible to do?

I am begging for help, really

What you can do is get both fields on load. It would look something like this.

$w("#dataset1").onReady(()=> {
    $w("#repeater").onItemReady(($item, itemData, index)=> {
        let firstName = itemData.firstName
        let lastName = itemData.lastName
        $item("#textField).text = firstName + " " + lastName
    })
})

The " " is to add a space in between both.

Let me know if this helps.

I’m not sure I understood this correctly (not into coding at all, and french, I’m sorry), but I got this message:
Unexpected token, expected “,” (7:0)
5 | $item(“#prenomnom”).text = prenom + " " + nom
6 | }

7 | }
| ^

@cabinetsexologie16 Can you copy and paste the whole code you wrote on page? That will help me better. It is definitely a formatted issue.

@jarod
Here it is ! I probably did it wrong… As I said, I’m not into coding at all and wasn’t sure if I had to modify what you gave me or if I had to copy as it was.
Fyi: “temoignages” is my database, “prenom” is first name and “nom” is last name

$w ( “#temoignages” ). onReady (()=> {
$w ( “#repeater1” ). onItemReady (( $item , itemData , index )=> {
let prenom = itemData . prenom ;
let nom = itemData . nom ;
$item ( “#prenomnom” ). text = prenom + " " + nom
}
}

Copy and paste the code in like this

$w ( “#temoignages” ). onReady (()=> {
$w ( “#repeater1” ). onItemReady (( $item , itemData , index )=> {
let prenom = itemData . prenom ;
let nom = itemData . nom ;
$item ( “#prenomnom” ). text = prenom + " " + nom
})
})
You just forgot the ) after the closing brackets.

@jarod Oh, that’s why! I’ll try rn and tell you if it worked!

@jarod So it seems to work, but I got a message saying $w could not be executed until page is ready. How do I have to write the “on ready” thing in the code?

Sorry for confusion this code should go into the page onReady.

So it should look like this.

$w.onReady(()=> {
ALL CODE FROM BEFORE IN HERE
})

@jarod It all seems to work well! Thank you so much, really!!!

Looks like you need a semicolon at then end of your line.
$item ( “#prenomnom” ). text = prenom + " " + nom;

Semicolons are actually not required in Javascript or Velo for that matter

Thanks for that, I 've always thought they were required, but you are correct, they get inserted automatically if not used. I come from many other languages that do require them. I noticed that if its a line break, they are not required. But best practice is to always use them. See the article at stack overflow. But to each his own. Because I’m constantly flipping between languages within the day, I often get hammered, by an interpreter/compiler if its missing.
https://stackoverflow.com/questions/537632/should-i-use-semicolons-in-javascript

I have actually been bitten with a javascript bug before when doing math computations, because the interpreter automatically went to the next line and include stuff that it should not have. Adrienne Miller has a great article showing when it can trip you up.

@pekrzyz
It is always better to work correctly and to generate consistent, clear and good formatted code.

Even if semicolons in Velo are not neccessary, coder should follow → Paul Krzyz suggestion.

When you code a lot, you will know why.

@russian-dima Agreed, consistency is key.