Need help with save data code

Hello,

I need help with the save button. How do I code this. The member enters the data in the field and it should get saved in the database I created (database name: Portfolio).

Attached is the screenshot.

Really appreciate your help

Hello Pinakin Sabnis,

you do not need any code for this.

Here the steps to do…

  1. Create a DATABASE called “Portfolio” (already done by you)
  2. Create in the Database following columns (name / lastname / socialmedia)
  3. Create a DataSet and connect it to the database “Portfolio”
  4. Set the OPTIONS in the DataSet to ----> “WRITE-ONLY”
  5. Go into the Database and activate all “PERMISSIONS”
  6. Create needed inputfields and buttons (already done by you)

Now you should have a ready “DATABASE” which is connected to the created “DATASET”. All “PERMISSIONS” are activated and the “DATASET” is set to “WRITEONLY”.

  1. Now connect all your “INPUTFIELDS” to correct columns in your DATABASE/DATASET".

  1. Connect at last your “Save my information”-button also to the DATASET/DATABASE (choose the first option, it should be something like → INPUT).

If you still need some help, look here —> https://mt2-king.wixsite.com/website/blank-3

As you can see ----> NO CODE NEEDED !

hello…Many many thanks for your response my friend…Suppose if he wants to edit the info for instance his social media ID…Will that over-right the previous one using these steps…

Again many thanks for the reply.

this may help

Hello Pinakin,

you need still more Help?

I think your next problem is the following…

You want to have 2-options right?

  1. Create a new INPUT-DATA
  2. Replace the old DATA by overwriting with the new one?

Is that what you want to do?

If it is like that, so i think you will not be able anymore to do that without any CODE.
Then you are forced to use some CODE, because in my example it is only possible to collect new lines of data to the database.

To replaces already existing data in a DataCollection you could use some something like this one…

https://www.wix.com/corvid/reference/wix-data.html#save

Thanks dear…that would help me a lot! :slight_smile:

My POST deleted again ???

Hello Pinakin, did you get my second answer?

First at all, i want to ask, if you did understand the first part of may “help-example” ? ? ? Did it work for you?

My second question is, if you need more options and want to do it by CODE?

And another of my questions was:

["You want to have 2-options right?

  1. Create a new INPUT-DATA
  2. Replace the old DATA by overwriting with the new one?

Is that what you want to do?"]

I have send you a link, which shows you how to solve this problem.

You can use several options…


Examples

Save an item in a collection
Copy Code

import wixData from'wix-data';// ...let toSave ={"title":"Mr.","first_name":"John","last_name":"Doe"};  wixData.save("myCollection", toSave).then((results)=>{let item = results;//see item below}).catch((err)=>{let errorMsg = err;});

Examples

Insert an item into a collection
Copy Code

import wixData from'wix-data';// ...let toInsert ={"title":"Mr.","first_name":"John","last_name":"Doe"};  wixData.insert("myCollection", toInsert).then((results)=>{let item = results;//see item below}).catch((err)=>{let errorMsg = err;});

Should look somethink like this…

import wixData from 'wix-data';

$w.onReady(function () {

});


var DATABASE = "Portfolio" // <--- Enter here the name of your Database

function SAVE_DATA (){
 let DATAtoSave = {
 "first_name":   "John",
 "last_name":    "Doe",
 "socialmedia":  "My social media"
    };
    wixData.save("DATABASE", DATAtoSave).then( (results) => {let item = results;});
}

Then you have to call this function with an click-event.
This is just an example, you have still to optimize the code by yourself.

Hello My Friedn…I got the first answer but hat is not what I was looking for. The second one which you have sent. Let me try that and I will get back to you on the same forum. Thanks :slight_smile:

@russian-dima

import wixData from ‘wix-data’;
$w.onReady(function () {
})
var database = “Portfolio”

function save_data(){

let datatosave = {
“fullname”:$w(‘#fullname’).value,
“aboutme”:$w(‘#aboutme’).value,
“fb”:$w(‘#fb’).value,
“insta”:$w(‘#insta’).value,
“twitter”:$w(‘#twitter’).value,
};
wixData.save(“database”, datatosave)
.then( (results) => {let item = results;});
}

export function save_click(event) {
$w(‘#dataset1’).save(‘fullname’,$w(‘#fullname’).value);
$w(‘#dataset1’).save(‘aboutme’,$w(‘#aboutme’).value);
$w(‘#dataset1’).save(‘fb’,$w(‘#fb’).value);
$w(‘#dataset1’).save(‘insta’,$w(‘#insta’).value);
$w(‘#dataset1’).save(‘twitter’,$w(‘#twitter’).value);

}

@russian-dima The above code worked successfully…thanks to you…Only concern is where to add wix.location which would redirect it to the portfolio page

Hallo again,
nice to hear that you have found a solution for your problem.

The question is, [when] do you want that the REDIRECTION happens?

After pressed “Save-Button” ?

If so, then you will need something like this one…

import wixLocation from 'wix-location';

// ...

wixLocation.to("http://wix.com");

You have to connect it to an event-handler -----> OnClickButton [SAVE]

export function myButton_SAVE_click(event) {
    
    .
    . . .here your CODE . . .
    .
  
    wixLocation.to("http://google.com");
}

So, when you press the “SAVE”-button, first run your own CODE, then at the end there will be the redirection.

Perhaps you will have to use something like —> .then(()=>{ }) or setTimeout(()=>{ … },1000) to make CODE running sequential, but not sure.