Use user input in backend function and display function result in textbox. [Solved]

This issue I had great difficulty with hence this post.

The idear:
An user inputs some number and this input is used in a simple multiplying function, by clicking a button. The backend function looks like the following:

The two input fields are labeled #input1 and #input2, this is the standard name then creating them.
The text fields is labeled #text1 also a standard name.

export function multiply(factor1, factor2) {
let product = factor1 * factor2;
return product;
}

Simply taking factor1 and multiplying it with factor2. The frontend looks like this:

import {multiply} from ‘backend/SimpleTestFunc’;

export function button1_click(event) {
multiply($w(“#input1”).value,$w(“#input2”).value)
.then(product => {
console.log(product)
product = product.toString();
$w(“#text1”).text = product
})
. catch (error => {
console.log(error);
});
}

The event of button 1 is a click. (The first line in the frontend code).

Note here that the console.log(product) is not necessary for the code to work, though it is nice to see the output in the case that something happens to the textbox.
A textbox contains a string, this is why the “product = product.toString();” is in the code, the textbox can’t contain a number. This turns the result of the backend function called “product” into a string.
$w is necessary for the code to access the fields: the textbox, the user input… After the call of each input a “.value” is there because the code must know that kind of input it can expect. In our case a number. The same goes fore the textbox. It’s text we enter into the textbox.

I hope this helped.

Thanks, Erik.

You could simplify your button1_click function a little by replacing the following two/2 lines:

         product = product.toString();
         $w("#text1").text = product 

with the following line:

         $w("#text1").text = product.toString();

How would I be able to display a product from my wix store with the results given from the text input?

Is the textbox a user input or a button.

Perhaps looking into a search function would be ideal.

@erik Good afternoon Erik, the textbox is a user input. I have a button right underneath,called “FIND BATTERY”, which, when clicked, take the user input from the text box and displays the year, make and model (which is found through the api). What I would like to do is have the results (year, make and model) connected to my database or store so that the customer can easily purchase the correct battery to their vehicle. I started creating a database with every vehicle from the year 2000+ and have a product reference field within the database. My question is how would I be able to connect that data with the results given?

@tsuyoidesigns
This is a video walking you though steps, it’s a bit circumstantial to walk though the steps in text.