Reputation: 666
Can you help me please, I have a flask app with some javasript. I need to send the data through post to python and call javascript function. But when I try to call JS first on flask "action" event, my post data was sending empty to python, after calling JS function. For my features I need this function to show the alert message and erase the text fields!
So, the main idea is show an alert "Your message has been sent!", clear text fields and send post data when you click the button!
Here is the code (JS):
<form action="/sendemail" method="post">
<input type="text" id="email" name="email" placeholder="Email" />
<textarea name="message" id="text" placeholder="Message"></textarea>
<button id="button" onclick="myFunc()">Send</button>
</form>
<script>
function myFunc() {
alert("Your message has been sent!");
document.getElementById("email").value = "";
document.getElementById("text").value = "";
}
</script>
Python:
@FlaskApp2.route('/sendemail', methods=['GET', 'POST'])
def MailSend():
print request.form['email']
print request.form['message']
return ('', 204)
And after all of these I have empty data from post request, I think because JS is execute faster than flask "action" block!
How I need to catch it and send post data before JS call the function on a button click event?
Upvotes: 1
Views: 471
Reputation: 995
You javascript function myFunc is executed before form submitted to the server. So, the email and message have already cleared.
I think you can check this answer: Clearing my form inputs after submission
Upvotes: 1