I am using the below code snippet to call a function when a button is clicked

<a class="btn btn-primary btn-sm" role="button"  style="display: block; margin: 0 auto;" onclick="RecordFunction()">Record</a>

The function is define below as

<script type="text/javascript">
function RecordFunction (){
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "/record/{{name}}", true);
  xhttp.send();
  alert("Thank you ");
}
</script>

But it is throwing an error

Uncaught ReferenceError: RecordFunction is not defined at HTMLAnchorElement.onclick

When I searched the error I found out it may be because the function is not loaded so I tried defining it before the button div and also tried this. But did not work. Any help is appreciated.

My whole index.html is like below :

<!doctype html>
<html>

    <head>    

<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

  </head>
   <body>

<div class="jumbotron text-xs-center">
  <h1 class="display-3">Thank You {{ name }}</h1>
  <p class="lead"  style"text-align:center"><strong>click the below button </strong></p>
  <hr>
  <div class="col-md-4"></div>
  <div class="col-md-6">
  <p class="lead">
    <a class="btn btn-primary btn-sm" role="button"  style="display: block; margin: 0 auto;" onclick="RecordFunction()"> Give Consent  </a>
  </p>
</div>

<div class="col-md-4"></div>
</div>
<!-- </div> -->
<div class="bgImgCenter"></div>
<script type="text/javascript">
function RecordFunction (){
  var xhttp = new XMLHttpRequest();
  xhttp.open("POST", "/record/{{name}}", true);
  xhttp.send();
  alert("Thank You ");
}
</script>

   </body>
</html>

I am rendering this by a flask template

@app.route('/<user>')
def hello(user):
    name=user
    return render_template('index.html', name = name)

0 Answers