Form submit button not working

form = st.form(key=‘my-form’)
name = form.text_input(‘Enter your name’)
submit = form.form_submit_button(‘Submit’)
st.write(‘Press submit to have your name printed below’)
if submit:
st.write(f’hello {name}')
st.write(“hi”)

when i am using the above code directly the hi is priting and when i click on submit button it is returning to the initial stage of the web app.

It works for me. A more conventional (neater) way of structuring the code is as follows:

import streamlit as st

with st.form(key='my-form', clear_on_submit=True):
    name = st.text_input('Enter your name')
    submit = st.form_submit_button('Submit')

st.write('Press submit to have your name printed below')

if submit:
    st.write(f'hello {name}')

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.