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?
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?
Share Improve this question asked Jul 30, 2012 at 21:43 creamcream 1,1295 gold badges16 silver badges26 bronze badges 4- 1 Bind a submit handler to the FORM element. – Šime Vidas 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? – Barend 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. – cream Commented Jul 30, 2012 at 22:30
-
Yep, that's correct! The web browser executes the
onsubmit
handler and when the handler pletes, the web browser sends the HTTP request to your web server, which forwards it to PHP. Fun fact: if you returnfalse
from your onsubmit handler, the web browser aborts the form submit and never sends the HTTP request. – Barend Commented Jul 31, 2012 at 7:03
2 Answers
Reset to default 3Yes, 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 :)
Here is a non-jQuery jsfiddle version, that uses the onSubmit handler.