mercredi 7 août 2019

how can i get sum of selected checkbox and save selected item in batabase mysql

am trying to create a web page for the menu in a restaurant that contains menu (name and price of food). What I want is in the front end when user select menu by clicking the checkbox, he gets the total of price on the webpage. Also, to be able to extract the names of the menu he selects and saves them on MySQL database using python flask.

What I have done is to create a two list from database one is for names of food and the other one is of the prices but what I get is this. the result i get

def ef menu1():
    cur = mysql.connection.cursor()
    cur.execute('''SELECT name FROM menu ''')
    name = cur.fetchall()
    cur.execute('''SELECT price FROM menu ''')
    price = cur.fetchall()
    cur.execute('''SELECT MAX(num) FROM menu''')
    maxi=cur.fetchone()
    maxid = int(maxi[0])

    return render_template('test4.html',name = name,price=price , maxid=maxid)

<div class= "row" style="background-image:' url('');">'
        <div class= "col-lg-6">
            <br>
            <p>
                <strong>Food</strong> <br />
            
            </p> 
            <p>
                <strong>Amount (US$)</strong>: <input type="text" name="amount" id="amount" />
            </p>
        </div>
    </div>

<script src="" ></script>
    <script src=""></script>
    <script type="text/javascript">
        $(document).ready(function() {        
            $(".my-activity").click(function(event) {
                var total = 0;
                $(".my-activity:checked").each(function() {
                    total += parseInt($(this).val());
                });
                if (total == 0) {
                    $('#amount').val('');
                } else {                
                    $('#amount').val(total);
                }
            });
        });    
    </script>




Aucun commentaire:

Enregistrer un commentaire