1

how would I execute a JavaScript function on a PHP form before submit but after all text is entered?

let's say I want to make all the text uppercase or something.. would onsubmit execute before the PHP takes over?

asked Jul 30, 2012 at 21:43
4
  • 1
    Bind a submit handler to the FORM element. Commented Jul 30, 2012 at 21:45
  • 1
    "would onsubmit execute before the PHP takes over?" --> That's such a great learning question that it'd be criminal not to turn it around on you (honest). In what order do you think onsubmit and PHP will happen? Why? Commented Jul 30, 2012 at 21:49
  • 1
    the only time i've ever really seen this used is to create an alert saying "thank you for your submission" or something... which seemed simultaneous, but in retrospect the javascript would have had to execute before the php refreshed the page. thanks! i feel smarter already. Commented Jul 30, 2012 at 22:30
  • Yep, that's correct! The web browser executes the onsubmit handler and when the handler completes, the web browser sends the HTTP request to your web server, which forwards it to PHP. Fun fact: if you return false from your onsubmit handler, the web browser aborts the form submit and never sends the HTTP request. Commented Jul 31, 2012 at 7:03

2 Answers 2

3

Yes, onSubmit event will be before PHP. Here is jsFiddle example with jQuery. Of course you can use native event handler or whatever you like. I use that, bacause it is faster :)

answered Jul 30, 2012 at 21:57
Sign up to request clarification or add additional context in comments.

Comments

2

Here is a non-jQuery jsfiddle version, that uses the onSubmit handler.

answered Jul 30, 2012 at 22:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.