Can't find the right coding for calculation - URGENT!

Hello everyone!


In the picture above, there are two sections:

  • 3 bars for user input (select date, select number, submit), connected to a database.
  • 2 text zones (show dates, available tickets), each with 6 text fields.

I would like to achieve the following: each time a user submits the form, the number of available tickets for the selected date has to decrease with the number of ordered tickets for that date.

Example: I want 2 tickets for the show on Friday 20 September 2019. When I submit the form, the number under available tickets for that specific date should go from 86 to 84 (when the page has reloaded).

I’ve been trying so many things already, but it seems to me that I’ll need some more advanced coding for this. Can someone please help me with this?

Thanks a lot in advance!

Put your show dates and available tickets in a database.

When the user selects a particular date, query the database to get total amount of tickets available.

When the user enters how many tickets they want, verify that the number of tickets the user wants is not greater than the total amount of tickets available.

Once the user purchases the tickets update the database by subtracting the number of purchased tickets from the total number of tickets available.

Thanks a lot for your reply Mike, very much appreciated!

What you describe makes a lot of sense to me, but the problem is that I don’t really know what code functions I have to use for this (as I am far from a professional when it comes to this).

Could you possibly help me with this by translating your reply to code language (more or less)?

Thanks a lot!

@michieldw
How does the purchase transaction take place once the user presses submit on your website ?

@mikemoynihan99 Well, users are only supposed to use this online form to make a reservation. They pay their actual ticket at the theatre itself. I hope that makes it even easier?
Thanks again for looking into this!

@michieldw what prevents a person(s) reserving all the tickets and then not paying for them thus preventing genuine buyers from paying for tickets

@mikemoynihan99 if necessary, I thought about limiting the number of tickets to 5 per submit. It’s just a reservation tool, very similar to the one we were using on the old website (one I didn’t make), and there that worked well. Just have trust in our audience, I guess :wink:

@mikemoynihan99 The main goal of solving this problem is to always have to most up to date number of available tickets displayed, and that for every show date. It’s just informative, both for audience as for administrators.

@michieldw

Ok so start by putting your show dates and available tickets in a Database.

Then connect the Database to a Repeater on your webpage. The repeater will show the number of tickets available for each date.

https://support.wix.com/en/article/about-displaying-database-content-in-a-repeater

@mikemoynihan99 thanks, this might indeed help for the display!

But what concerns the calculations, what code functions do I have to use to - based on what you said in your very first reply - run the query, verify the total ticket number and perform the substraction?

Could you possibly write me an example? I know I’m asking a lot, but my website is ready to be launched as soon as I solved this problem - I am really struggling with it…

@michieldw
Next you need to set up your date selector so that it only allows selection of the show dates specified your database.

@michiel.dw

Then you would code something like this…

import wixData from ‘wix-data’;

$w.onReady(function () {

//change element IDs and Field Keys as necessary for your specific website

        $w('#submitButton1').onClick((event) => { 

            
            let chosenDate = $w("#dropdown1").value; 

            console.log(chosenDate); 

            let numberOfTickets = $w("#input1").value; 

            console.log(numberOfTickets); 

            wixData.query("database1") //enter database name here 

                .eq("date", chosenDate) //make sure that you have a field key in your database called date, and the field key is type TEXT  

                .limit(1) 
                .find() 
                .then((noob1) => { 

                    console.log(noob1); 

                    let result = noob1.items[0]; 

                    console.log(result); 

                    let ID = result._id 

                    console.log(ID); 

                    let numberOfTicketsAvailable = result.numberOfTicketsAvailable //make sure that you have a field key in your database called numberOfTicketsAvailable, and the field key is type NUMBER 

                    console.log(numberOfTicketsAvailable); 

                    if (numberOfTicketsAvailable >= numberOfTickets) { 

                        let toUpdate = { 
                            "_id": ID, 
                            "numberOfTicketsAvailable": (numberOfTicketsAvailable - numberOfTickets), 
                            "date": chosenDate, 
                        }; 

                        wixData.update("database1", toUpdate) 
                            .then((noob2) => { 

                                console.log("tickets successfully reserved lad"); 


                                console.log("number of tickets now remaining = ",(noob2.numberOfTicketsAvailable)); 


                           }); 


                               } else { 
                                    console.log("not enough tickets available for your selected date"); 
                                } 

                            }) 

                }) 

        })