Skip to content
Advertisement

Load data from Flask/Python to HTML textarea

I am trying to send data from Python / Flask to an HTML form textarea but need some help.

responses.html is shown below.

JavaScript

A portion of app.py is shown below:

JavaScript

I would like to display any previously recorded responses in the response textarea of responses.html while preserving the placeholder when no previous response was received from the user.

Advertisement

Answer

I suggest you replace input tag with textarea tag. Then you can add responses in textarea like this (p.s. you need to format responses, this example will just display SQL result):

JavaScript

If you still want to use input tag (although input type='textarea' is not valid HTML tag, you should switch to type='text') then you should add value attribute, like this:

JavaScript

Both approaches will populate the input/textarea with responses if there is any response, otherwise, it will show a placeholder

Also, I’ve noticed that you have an error in SQL code, you always replace the answer with the newest one so you’ll have just one answer in DB.

Advertisement