1

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?

4
  • Do you want to submit the form (which reloads the page) or just send the data to the server in the background and then clear the fields and show a message? Commented Sep 27, 2017 at 5:51
  • Exactly, just send the data to the server without reload! Commented Sep 27, 2017 at 5:52
  • You javascript function myFunc is executed before form submitted to the server. So, the email and message have already cleared. Commented Sep 27, 2017 at 5:53
  • I know, how I need first send the data, and then clear the fields? Commented Sep 27, 2017 at 5:54

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.