Ranjit Mishra
Ranjit Mishra

Reputation: 128

Not getting selected radio button value from one HTML form to another in Python Flask

I have a home page with radio buttons. I select an item (database type) and want to get the checked-value in python code and then after some validation want to display another form to connect to the selected database type. But after trying so many things, I am unable to get the selected-radio-button value and then unable to process conndb.html below. I am quite new to Python web development. Not getting anything working to meet this requirement. Home page pyhton code is

@bp.route('/')
def index():
db = get_db()
if request.method == 'GET':
    posts = db.execute(
            'SELECT id, DB_Tp'
            ' FROM DB_Type'
            ' ORDER BY DB_Tp ASC'
            ).fetchall()

    error = None
    if len(posts) == 0:
        error = 'No Database Type To Choose For Meta Data. Create New Entry'
        flash(error)

    return render_template('meta/index.html', posts = posts)
else:           **#=> from here the code is never executed. It always falls thru to next function**

    checked = request.form['dbtype']
    print("This is ", checked, flush = True)
    if checked == "MySql":
        return render_template('meta/conndb.html')
    else:
        error = "Nothing checked "
        flash(error)
        return redirect(url_for("meta.index")) 

index.html is -

{% extends 'base.html' %}

{% block header %}
  <h1>{% block title %}Select Database Type{% endblock %}</h1>

{% endblock %}

{% block content %}
   <form class="post" action="/conndb">
{% for post in posts %}
    <label class="body">
    <input type="radio" name="dbtype" required>{{post['DB_TP']}}</label><br>
{% if not loop.last %}
{% endif %}
{% endfor %}    
    <button type="submit">Next</button>
</form>
<hr>
{% endblock %}

Depending on the selected radio button, I want to perform different operations with different kind of DB. After getting the value from the selected radio button, I render this template to connect to database... conndb.html

{% extends 'base.html' %}

{% block header %}
   <h1>{% block title %}Provide Details To Connect To Database{% endblock %}</h1>
{% endblock %}

{% block content %}
 <form method="post">
 <label for="host">Host Name</label>
 <input name="host" id="host" required>
 <label for="user">User ID</label>
 <input name="user" id="user" required>
 <label for="passwd">Password</label>
 <input type="password" name="passwd" id="passwd" required>
 <label for="db">Schema</label>
 <input name="db" id="db" required>
 <input type="submit" value="Get MetaData">
 </form>
{% endblock %} 

Any help is greatly appreciated.

Upvotes: 0

Views: 598

Answers (1)

xpeiro
xpeiro

Reputation: 760

"value" attribute is missing in radio html.

e.g.:

<input type="radio" name="dbtype" value="{{post['DB_TP'].id}}" required>{{post['DB_TP']}}</label><br>

Upvotes: 1

Related Questions