Issue making an AJAX call to flask render_template

Issue making an AJAX call to flask render_template

Suppose that I have two radio buttons (labeled 1 and 2) and some text at the bottom of the page. By default the value is&nbsp;-1&nbsp;for the text and no check boxes are selected. If I click one of the radio buttons I want to change the value of the text to be either 1 or 2 depending on the radio input that was selected. To do so I have based the code on the&nbsp;<a href="https://stackoverflow.com/a/24891804/667648" target="_blank">AJAX call described here</a>. Here is the code:

Suppose that I have two radio buttons (labeled 1 and 2) and some text at the bottom of the page. By default the value is -1 for the text and no check boxes are selected. If I click one of the radio buttons I want to change the value of the text to be either 1 or 2 depending on the radio input that was selected. To do so I have based the code on the AJAX call described here. Here is the code:

hello.py

from flask import (
    Flask, render_template, request
)

app = Flask(name)

@app.route('/', methods=('GET', 'POST')) def hello(): level = -1 print(request.method) if request.method == 'POST': level = int(request.form.get('level', False)) return render_template('test.html', value=level)

templates/test.html

<html>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
  <body>
    <form>
      <input name="level" type="radio" id="1">1</input>
      <input name="level" type="radio" id="2">2</input>
    </form>
    {{ value }}
  </body>
  <script>
    $(function() {
      $('input[type=radio]').change(function() {
        console.log( $( this ).attr("id"));
        $.ajax({
          type: "POST",
          data: { level: $( this ).attr("id") },
          success: function(response) {
            console.log("HERE");
            document.write(response);
          },
        });
      });
    });
  </script>
</html>

When I call flask run, selecting either radio button will change the value to either 1 or 2 but I cannot select either radio button a second time. The page will hang on the second selection, and I am not sure what is going on.

Note: Although this may seem like overkill, I have a more complex form submission in the larger project I am working on, this is simply a MVCE.

javascript python html flask

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Know Everything About HTML With HTML Experts

HTML Assignment Help Australia @30% OFF from Sample Assignment, with Our Best HTML assignment help experts. Get HTML homework help online at affordable price. 100% Plag free assignment solution.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

HTML JavaScript - Add Javascript File to HTML

Learn HTML and javascript, their uses & importance, html javascript function, javascript tags list, why add javascript file to html, HTML Events with JavaScript etc

Python Flask - Introduction to Flask Templates

This is our second tutorial in Python Flask, in this tutorial we are going to have Introduction to Flask Templates, so for this Flask looks for the template

Python Flask-Mail Library to Send Emails in Browser Using Flask Full Project For Beginners

Python Flask-Mail Library to Send Emails in Browser Using Flask Full Project For Beginners #python #flask #flaskmail Welcome Folks My name is Gautam