Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Counting html button clicks in JS front end and sending it to flask backend

I am trying to track the number of times a button or a dropdown is clicked in my flask app and send it back to python backend so i can input into database. I keep getting null for my value i cant seem to figure it out.

new.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form method = "POST">
        <button type="button" id = "numberclicks" name = "numberclicks" onClick="onClick()">Click me</button>
        <p>Clicks: <a id="clicks" name ="clicks" placeholder = 0 >{{clicks|safe}}</a></p>
        <button type="submit">Login</button>
    </form>
    </div>
    <script>
        var clicks = 0;
        function onClick() {
           clicks += 1;
           document.getElementById("clicks").innerHTML = clicks; 
           console.log(clicks)
        }   
        </script>

</body>
</html>

app.py

from flask import Flask,render_template,request

import json

app = Flask(__name__)



# turn this into calss and import to record who is logged in to the session aka. user then take log data and make relationship to tables
# HOW MANY TIMES USER VISITED THE VISIT PAGE!!! 



@app.route('/', methods=['POST', 'GET'])
def signUpUser():
    if request.method == "POST":
        click = request.args.get('clicks',type = int)
        return json.dumps({'status':'OK','click':click})
    return render_template("new.html")

if __name__ == "__main__":
    app.run(debug=True)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could use the XMLHttpRequest object to send the clicks count asychronously to the flask backend.

const xhr = new XMLHttpRequest();
xhr.open("POST", "/", true);
xhr.onload = () => {
    if (xhr.status === 200) {
        ...do whatever you want...
    }
}

const data = new FormData();
data.append("clicks", clicks);

xhr.send(data);
about 4 years ago · Juan Pablo Isaza Report

0

function onClick() {
  clicks += 1;
  document.getElementById("clicks").innerHTML = clicks;
  console.log(clicks);
  fetch("/", {
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
    method: "POST",
    body: JSON.stringify({clicks}),
  });
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!