Auto-Updating Text Field

Hi all,

I’m trying to make a text field act as a counter, and update whenever people press a “Submit” button. The counter works, but the user has to refresh the page to update the counter, which isn’t very intuitive.

I have a text field that counts the number of fields in a dataset and reflects them as a counter. (Shown as “32” below).

The number of fields in said dataset indicate how many people have submitted their email address to us.

The current code I’m using is:

$w.onReady( () => {
    $w("#dataset2").onReady( () => {
        $w('#text50').text = $w('#dataset2').getTotalCount().toString()
} );
} );

The dataset is #dataset2, while the text field above is #text50.

How do I make this number automatically whenever people “submit” their emails to us through a “submit button”? #yesButton

Thanks for your help!

I’m not sure what you’re asking. Is something wrong with the code you posted? Doesn’t it work?

Hi J.D., the code I posted doesn’t update live, only upon manual refresh.

So if the user submits their email address, they won’t see the number go from 32 → 33 unless they refresh the page. I’m looking for a way to make that happen automatically.

Thanks!

Currently you have the code only being run at the same time as the page loads itself and so you will need to refresh the page to get this code to rerun itself.

If you want to refresh it when they do the submit, then you can look at adding the refresh to the submit function in your code.

You can see an example in a previous forum post here.
https://www.wix.com/corvid/forum/community-discussion/onclick-submit-data-and-reload-the-page-how-to

Hi GOS,

I’ve seen that post before and tried an adaptation of it:

export function yesButton_click(event, $w) {
    $w("#dataset2").save()
        .then ( ( ) => {
        $w("#dataset2").refresh();
    })
    .catch((err) => {
        let errMsg = err;
    });
}

but it doesn’t work unfortunately. Is there a problem with the code, or is there something wrong elsewhere?

Thanks for the help!