开发者

Have form do two things on submit

开发者 https://www.devze.com 2022-12-26 23:58 出处:网络
Hey, I\'m just wondering if it\'s possible to have a form in html do two things o开发者_如何学Pythonn submit, have the action go to a url like normal (for a search) but also run a mysql command.

Hey, I'm just wondering if it's possible to have a form in html do two things o开发者_如何学Pythonn submit, have the action go to a url like normal (for a search) but also run a mysql command.

Thanks!


A form can not run any SQL. HTML has nothing to do with databases, it doesn't know about them, it can't talk to them.

A script on your server can do SQL queries, and that script can be invoked by the browser request. The browser may also submit form data together with the request if it wishes.

It works like this:

  1. User submits form
  2. Browser generates a request and sends it to the given URL
  3. Server receives request, starts up script corresponding to the URL
  4. Script queries database, does something with the received form data
  5. Script outputs some reply
  6. Browser receives reply (website) and displays it


Stop. Never have a client ever run a MySQL (or any database) command. They could do a lot to seriously destroy the integrity of your database. You should have your processing page do the MySQL command after validating the input.

To answer your question, however, it is possible. Your form, upon submit, can call a Javascript function, which in turn does the two actions. But if you're doing this, there's probably some code that needs to be refactored.


I think you have a couple of options here. If your page requires javascript to be enabled for them to submit the form, then you could do an synchronous XMLHttpRequest call before the form submission. If you don't know if Javascript is enabled on the browser or there is a chance that someone can turn it off, then you would need to perform the two actions on the server after they submit the form.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号