I have a form on my page that has multiple yes/no drop downs with values, I can get all the selections to all up into a textbox (Answer) but it will not send the value in the textbox to the database. The Answer label is connected to the database but the only way it will save to the database is if i manually type in the field. How the form works is the member can change values by selecting yes or no then I have placed a get total button that adds all the fields up for a total then the submit button sends all the changes to the DB. When I go to the DB all the changes are there except the field that is to hold the total point value. The code below is how I am getting my total. It is almost like the total is there but looks blank to the DB.
export function button2_click(event, $w) {
let val1 = parseInt($w("#value1").value, 10)
let val2 = parseInt($w("#value2").value, 10)
let val3 = parseInt($w("#value3").value, 10)
var ans = val1 + val2 + val3 $w("#answer").value = String(ans); }
OK lets see if we can get you sorted then :-).
1. You need to make sure that all drop downs have been set. There are several ways to do this but I'll show you a simple basic way. First we will create a list of the elements that you have on the page which we will use to calculate the answer and validate that all drop downs were set.
const buildPointElements = [ $w("#value1"), $w("#value2"), $w("#value3"), $w("#value4"), $w("#value5"), $w("#value6"), $w("#value7"), $w("#value8"), $w("#value9"), $w("#value10"), $w("#value11"), $w("#value12"), $w("#value13"), $w("#value14"), $w("#value15"), $w("#value16"), $w("#value17"), $w("#value18"), $w("#value19"), $w("#value20"), $w("#value21"), $w("#value22"), $w("#value23"), $w("#value24"), $w("#value25") ]; // Initialize the counters let totalAnswerCount = 0; let answerTotalValue = 0; $w.onReady(() => { // Initialize drop down onChange handlers initOnChangeHandlers(); }); // initOnChangeHandlers loops through the build points elements list // creating onChange handler instances for each element function initOnChangeHandlers() { // Use the array forEach function to process each element buildPointElements.forEach((element) => { // Create onChange() event handler for the selected element element.onChange((event) => { // When the value of the drop down changes... // Re calculate the answer updateAnswer(); }); }); } // updateAnswer loops through each element doing two things // 1. It calculates the cumulative value of the element dropdowns. // 2. It checks how many elements have been set function updateAnswer() { // Loop through the elements and checking the values buildPointElements.forEach((element) => { // If the dropdown has a selected item then we add its value to // the total value and increment our answer count if (element.selectedIndex >= 0) { // A value has been set // Add the drop down value to our cumulative answer answerTotalValue += parseInt(element.value, 10); // Increment the Answer Count - we need all buildPoint // elements to be set totalAnswerCount++; } }); // Update the answer element $w('#answer').value = answerTotalValue; // If we have answered all questions we can save the answer if (totalAnswerCount === buildPointElements.length) { $w('#submitPoints').enable(); } }
Now its not clear if you are saving each individual drop down value into the database or not. If you are then you really don't need to save the calculated total as well. You can calculate it from the saved drop doan values now simply by calling the updateAnswer() function in the $w.onReady() , and of course as the values are updated, like this.
$w.onReady(() => { // Initialize drop down onChange handlers initOnChangeHandlers(); updateAnswer(); });
If you aren't saving the dropdown settings then you need to remove the Submit binding from your $w('#submitPoints') button and create a mouse click handler
$w('#submitPoints').onClick ((event) => { // Save the answerTotalValue in the dataset current item // Note $w('#dataset1').setFieldValue('answer', answerTotalValue); $w('#dataset1').save() .then((savedObject) => { // if we get here the answer was saved console.log("Answer saved saved object = "+JSON.stringify(savedObject)); }) .catch((error) => { // If we get here then the save failed and we show the error in the console // If this happens it might be because the dataset item hasn't been // set due to an empty data collection. So you may need to call the dataset new() function immediately before the setFieldValue() function call above console.log('Error! '+ (error instanceof Error ? error.message + (error['stack'] ? ' '+error.stack : '') : error)); }); });
Hope this helps or at least inspires a solution. Please NOTE: I have made best guesses at some of your element names. So this code may not work as is and may need some adjustment if the dataset element does not have the id '#dataset1' or if the submitPoints button doesn't have the id of '#submitPoints' or if the data collection you are using doesn't have an answer column called 'answer'.
Hope this helps.
Hi I have a very similar problem to the one above. I want to save calculations to a database but because the text boxes produce a .value amount they will not update to the database using a Submit button. Can anyone help. I want to save the value of someones bmi to the database.
export function BMI_click(event) { let height = parseFloat($w("#height").value) * 0.01; let weight = parseFloat($w("#weight").value); let BMI = weight / (height * height); console.log("weight", weight); console.log("height", height); console.log("My BMI", BMI); $w('#txtBMI').value = Math.round(BMI * 10) / 10; }
Stcroppe, I have sent you an email
No problem, one of the challenges with trying to help is that I cannot see how you have wired up your page. So what datasets do you have? What data collections do they map to and what are the column key names in the collection? how are the dataset columns wired up to your page? For example how are the drop downs configured? Given that you are new to javascript development solving your problem may only be doable by someone with experience accessing the Editor and taking a look at the page details. I can do this with you in a Google Hangout session or if you give me a contributor role I can probably solve the problem directly (I don't work for Wix so don't have the privileges to help you out otherwise). My contact info is in my profile and I will do this without charge if you are interested.
Storope, I really appreciate all your time, the code did not work for me, but that is fine, I will figure it out eventually. I guess my lack of experience in Javascript prevents me from doing anything with the code. Again thank you for your time in trying to help with my situation.
You are awesome ! thank you so much. I will adjust as needed, I can not wait to learn Javascript to be able to do stuff like this on my own. I will reply with an out come !!
I appreciate all the help, I don't know how to do all that but I will look into it. I am new to Javascript so its all foreign to me right now.
I think Giri is correct but understanding how you have wired up the elements to your data set is key here.
I just connected to your site and registered with a test email address.
First of all I would also initialize all of the drop downs to No (0) So that the calculation results in a valid value.
When I tried to manually call $w('#dataset1').setFieldValue('answer', 250); [I guessed at the answer key property]. I got a dataset write error. the error said the dataset was read only. If your dataset is not called dataset1 then perhaps I was trying to write to the wrong dataset. Anyway perhaps using dataset new(), setFieldValue() and save() after you double check the dataset access might work. Alternatively unhook the dataset and use wix-data.insert instead :-)
If the problem is that the .value is not written to the db, it´s because, per documentation (https://www.wix.com/code/reference/$w.TextInput.html#value) .value does not change the value in the dataset. You have to use the dataset´s setFieldValue and then save it.
here is page address: https://www.trailraidersofswfl.com/rocks
page is set to (members Only)
import wixUsers from 'wix-users'; import wixData from 'wix-data'; import wixLocation from 'wix-location'; export function Button1_click(event) { wixLocation.to(`/Members/${wixUsers.currentUser.id}`); } export function button2_click(event, $w,) { $w("#button2").label = "DONE"; let val1 = parseInt($w("#value1").value, 10) let val2 = parseInt($w("#value2").value, 10) let val3 = parseInt($w("#value3").value, 10) let val4 = parseInt($w("#value4").value, 10) let val5 = parseInt($w("#value5").value, 10) let val6 = parseInt($w("#value6").value, 10) let val7 = parseInt($w("#value7").value, 10) let val8 = parseInt($w("#value8").value, 10) let val9 = parseInt($w("#value9").value, 10) let val10 = parseInt($w("#value10").value, 10) let val11 = parseInt($w("#value11").value, 10) let val12 = parseInt($w("#value12").value, 10) let val13 = parseInt($w("#value13").value, 10) let val14 = parseInt($w("#value14").value, 10) let val15 = parseInt($w("#value15").value, 10) let val16 = parseInt($w("#value16").value, 10) let val17 = parseInt($w("#value17").value, 10) let val18 = parseInt($w("#value18").value, 10) let val19 = parseInt($w("#value19").value, 10) let val20 = parseInt($w("#value20").value, 10) let val21 = parseInt($w("#value21").value, 10) let val22 = parseInt($w("#value22").value, 10) let val23 = parseInt($w("#value23").value, 10) let val24 = parseInt($w("#value24").value, 10) let val25 = parseInt($w("#value25").value, 10) var ans = val1 + val2 + val3 + val4 + val5 + val6 + val7 + val8 + val9 + val10 + val11 + val12 + val13 + val14 + val15 + val16 + val17 + val18 + val19 + val20 + val21 + val22 + val23 + val24 + val25; $w("#answer").value = String(ans); }
If you can do a few things it will be easier to help.
1. Post all of the code on your page not just the one you believe to be causing the problem.
2. Post a link to the page you are having problems with. Show the element bindings for #answeri in a screen shot if possible.
What are the settings On your data set? Is it set to read only, write only or read write ? What are the permissions on your data collection? Who can write to it? Admin, Member or anybody?
if any of these permission settings don’t match the page user then data won’t be saved either.
button2 is not data bound, it is only used to get total, I have a separate Submit button used to send the changes to the DB. I made the change you suggested and still no luck.
export function button2_click(event, $w, f) { $w("#button2").label = "DONE"; let val1 = parseInt($w("#value1").value, 10) let val2 = parseInt($w("#value2").value, 10) let val3 = parseInt($w("#value3").value, 10) var ans = val1 + val2 + val3; $w("#answer").value = String(ans); }
Initial syntax observation - add a ';' after+ val3 and move the value assignment to the next line...
var ans = val1 + val2 + val3; $w("#answer").value = String(ans);
Now if your button2 element is bound to the dataset Submit function then it is probably firing before you assign the ans to $w('#answer') element. You may want to unbind the button and use $w('#<datasetName>').save() instead?