-2

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)
Ricky
  • 2,662
  • 5
  • 25
  • 57

1 Answers1

0

Try this:

<p class="lead" style"text-align:center"><strong>click the below button </strong></p>

is missing a = , it should be

<p class="lead" style="text-align:center"><strong>click the below button </strong></p>

Agoose Banwatti
  • 410
  • 2
  • 13
  • Didn't work but I realised I am getting an error `Uncaught SyntaxError: Invalid or unexpected token` at this line when page is loaded `xhttp.open("POST", "/record/{{name}}", true);`. – Ricky Jun 02 '19 at 08:29
  • What does {{name}} evaluate to ? – Agoose Banwatti Jun 02 '19 at 08:38
  • that is the parameter I pass through the `return render_template('index.html', name = name)`. So i render this html by calling localhost:5000/api/john.Then the {{name}} will be john. – Ricky Jun 02 '19 at 08:42
  • see above answer. maybe this will work. The code seems to work fine for me whenever testing it. – Agoose Banwatti Jun 02 '19 at 08:49
  • I think I am getting an error now `var xhttp = new XMLHttpRequest(); url="/record/{{ name }}"; console.log(url); xhttp.open("POST",url, true);` when I do this at defining url . Can we pass the {{ name }} inside the onclick function ? – Ricky Jun 02 '19 at 09:15