matthew fabrie
matthew fabrie

Reputation: 103

Sending a POST request to python function via an HTML form

I am absolutely new to HTML and I was wondering if Someone could help me out. I tried looking for similar questions as mine but I didn't find anything quite like what I am looking for. Sorry if this question seems dumb or easy to you I am trying to learn! So basically I have an ID that I can access inside of my HTML file and I want to send it to my python function through a form, I have already managed to do this for buttons but never for a form. This is how I would send my ID to my function with a button:

<a href="{{url_for('validate_comment', item_id=msr.id)}}"> <--- the msr.id is my ID
    <button type="button">
        <div class="font-weight-bold">Ajouter un commentaire</div>
    </button>
</a>

And this works perfectly but i was wondering if there was any way to do the same with a form as I am not calling a function but doing an action instead. This is how my form looks:

<form id="commentaire_form" method="post" action="/testement"> <--- here is where I give the route but how can i send the ID to the function linked to this route?
    <textarea id='text' name="text"></textarea>
    <input type="submit">
</form>

If anyone could help me I would greatly appreciate it, thanks in advance :-)

Upvotes: 1

Views: 675

Answers (1)

Qrzysio
Qrzysio

Reputation: 1172

The below will make a POST request to the /testement route/URL. You can catch the id POST value in your python script.

<form id="commentaire_form" method="post" action="/testement">
    <textarea id='text' name="text"></textarea>
    <input type="hidden" name="id" value="{{ msr.id }}">
    <input type="submit">
</form>

Upvotes: 1

Related Questions