How to insert data into DB with jQuery/Javascript without using PHP?

Asked

Viewed 31,206 times

9

I am learning Javascript and jQuery and I am wanting to make a small site to test the insertion of data in a database. I have no knowledge in PHP, I wonder if it is possible to insert data in a database only with Javascript or jQuery. I would also like to know the most efficient/practical way you recommend for this task.

  • 2

    Remember that PHP is not the only way to interact with a database. The world of server-side programming is very diverse and you can use almost any programming language to generate your HTML and interact with the database.

  • It is possible to manipulate databases through Javascript, but the database has to be in the machine where the page will be opened, or by mapped network unit. In case the only browser I could use was internet explorer, the others block for security. But that gives, gives.

7 answers

16

Forget about that idea. Accessing the database directly via javascript would create very serious security problems in your application, after all with a simple firebug, anyone could change javascript or use the javascript console itself to put any SQL command, including a DROP DATABASE there.

And before that, you would have to turn to managing the database connection directly in javascript, and that won’t be easy, if that’s possible.

In addition, even if you can do javascript connect in the database, once javascript runs in the browser, you would have to expose the SQL server in a public network with the login and password can be obtained from javascript. And then you’ve already given your database to any hacker on the Internet to do whatever you want.

  • Thanks for the friendly information! What would be the best way to do this interaction with the database? What I intend to do is something very simple to train some things like HTML5, CSS3 and javascript, I want to make a small "database" with names of movies I’ve watched and those I still want to see. My girlfriend decided to make a list of these in the word! So I thought why not try to make a site with that same functionality only in a dynamic way? Only my main problem is with the database, I could do with localStorage but I want to learn how to use a DB.

  • 2

    The best way is exactly to put a back-end however simple it is, in PHP, Perl, Java, C# or whatever, which is where you will direct the front-end calls. And then the back-end goes into the database. It may seem somewhat boring and unnecessarily heavy initially, but it’s not.

  • I agree with the very pertinent reply by the way.

3

3

Hello I saw this question and I thought, I think I know what he wants.

Well you do not want to create a complex and laborious back-end to do some trainings as you spoke, and from what I realized would not like to use another language "World" for the same.

I have a simple and elegant (quick) suggestion that does not hurt the security of the application and works perfectly, Nodejs, for you who seem to be better familiar with javascript, you will be at home, simple learning curve and is a great tool.

Nodejs works very well with Mongobd (an object-oriented database in JSON format) and Mysql.

I recommend at least to take a look at the subject.

3


You said in a commenting:

What I intend to do is something very simple to train some things like HTML5, CSS3 and javascript, I want to make a small "database" with names of movies I’ve watched and those I still want to see.

If you are using a server

And I recommend you use one, look for a package like xampp.

Do you really want to make a database in Mysql? Is it to learn how to handle SQL, or to make a dynamic website? For something like this I would forget SQL and use a json file on the server. You pull the file to Javascript with jQuery ajax.

File example:

[
   { "titulo": "ET", ano: 1982 },
   { "titulo": "Indiana Jones", ano: 1981 }
]

If it’s all local

You should be using a server.

In this case, create a JS file with a literal array:

var dados = [
   { titulo: 'ET', ano: 1982 },
   { titulo: 'Indiana Jones', ano: 1981 }
];

2

Cannot use Mysql directly with javascript/jquery without a script running on the server side as previously stated.

But for simple use like yours, you can download a script like this REST-API and change the tables and fields you want to use: http://coenraets.org/blog/2011/12/restful-services-with-jquery-php-and-the-slim-framework/ The input and output format is JSON, easily used with jquery and all other MV frameworks (Backbone, Angular, etc)

Plus learn to play with REST, HTTP Requests and the like.

1

If you want to learn how to manipulate a database and return the result in HTML, I advise as the friends mentioned above that first, learn programming logic, later a server-side language (PHP,ASP.NET,JAVA,C#,PYTHON...), since start for JSON, Node.JS will require good experience and cause knowledge in terms of web programming! There is also HTML5 Indexeddb, where it is possible to manipulate in your own browser a more simplistic database, for you to learn can be interesting. Here is a tutorial about: http://www.html5rocks.com/pt/tutorials/indexeddb/todo/

I hope the content will be useful to you!

Hugs

0

Not, that is not possible, Javascript is a language of use client-side, so it is executed in the user’s browser, not having no relation with the server. Therefore, you do not have direct access to the database.

Javascript is basically used for the dynamic handling of content on the site through the browser and for executing requests.

  • 1

    There are several solutions that implement server-side Javascript as well. Node.js is a good example and could be used by OP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.