How can you connect database using javascript in HTML page?
-
-
Answer:
You can't connect directly from a browser to the database, as the browser doesn't have access. Even if you could, you shouldn't: If your Javascript sends SQL statements to the database, there is nothing stopping a random visitor from having fun by changing your SQL to "drop table", and your entire database is gone. What you do is setting up a server side web service (using PHP, Java servlets, Ruby on Rails or whatever) that accepts a URL, converts it to a DB query and returns the result as JSON. The primary thing from a security standpoint is that you never insert a string from the web into the SQL without validating it first. As an example, you could have something like this: --- browser javascript (using jQuery) --- $.getJSON("/productDetail.json?productId=123", function (data) { // do something with the data }); --- server (using Java servlet) --- void doProductDetail(HttpServletRequest req, HttpServletResponse res) { int productId = Integer.parseInt(...); // get product ID from query param String sql = ...; // generate the SQL String json = ...; // execute query, convert result to JSON res.write(json); // send to browser }
wiki.answers.com Visit the source
Related Q & A:
- How can I connect my Cox Motorola HD box to my HDTV?Best solution by Yahoo! Answers
- How can I connect my Shareaza?Best solution by shareaza.pongwar.com
- How can I permanently return to my original yahoo page?Best solution by Yahoo! Answers
- How Can I Connect To The Internet On My Psp Using A USB Cable?Best solution by askdavetaylor.com
- How can I connect my non-HD DirecTv receiver to my hdtv using an hdmi cable?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.