最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - execute function before submit - Stack Overflow

programmeradmin0浏览0评论

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 return false 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
Add a ment  | 

2 Answers 2

Reset to default 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 :)

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

发布评论

评论列表(0)

  1. 暂无评论