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:
- User submits form
- Browser generates a request and sends it to the given URL
- Server receives request, starts up script corresponding to the URL
- Script queries database, does something with the received form data
- Script outputs some reply
- 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.
精彩评论