views:

796

answers:

7

Hi

Is it possible to insert data in a database using javascript only.

if yes then please post a simple example of it.

Since javascript is a client side language and not a server side,i think its not possible.

But then how to do it.

A: 

Javascript is primarily a client-side language and has no access to a database without the use of a server side language to supplement it.

It is possible to use AJAX to post the data to the server, which would prevent the page from posting back, giving the appearance that the javascript had performed the action. But inevitably you will need something like ASP.NET, PHP, Ruby, ect. to capture the data and persist it to the database.

EDIT: Although not what I would consider a true database, there is a library called Taffy DB which is a client-side javascript database. Keep in mind that it is an in-memory database and it does not persist data to the server, so the data will be gone as soon as the user closes their browser. The Taffy DB FAQ covers it's ability to persist the data pretty well.

AJ
JS is NOT strictly client-side language. It is primarily used on the browser, but it IS a scripting language that has other uses. It's just not as common. For example, Adobe CS allows you to use js to interact with their programs.
Jim
Thanks for the catch Jim, I have updated my post to reflect that. I made an assumption that he was referring to javascript within a web browser.
AJ
A: 

Not directly. Any server-side language could be made to do this. Allowing end users to craft database queries however is just asking for it in the worst way.

Daren Schwenke
A: 

The short answer is "no", javascript is a client side language, and has no ability to connect to a DB.

HOWEVER, the long answer is 'yes, but...'. There are a number of socket-libraries available in javascript (though most I saw seem to involve some flash/java trickery). Therefore, one could connect to the sql database (which would have the obvious security flaw) and submit the record from there.

It would be POSSIBLE, but absolutely stupid, difficult, and insecure.

Erich
+1 for absolutely stupid, difficult and insecure.
Justin Johnson
-1 for absolutely stupid, difficult and insecure. Nobody is asking for shortsighted judgements here.
KooiInc
+2  A: 

It is possible if you are using CouchDb http://couchdb.apache.org/

Apache CouchDB is a document-oriented database that can be queried and indexed in a MapReduce fashion using JavaScript. CouchDB also offers incremental replication with bi-directional conflict detection and resolution.

Tzury Bar Yochay
A: 

Google: 'javascript odbc access'

First result

http://forums.asp.net/t/1285316.aspx

It IS possible. Though I don't know why you would want to do it. The code makes me think of when I did coding in ASP with ODBC support.

Jim
That is referencing a local connection only.
Daren Schwenke
It says "Local ODBC" at the pointed post.
Braveyard
He never specified which (local/remote) server he needed to access or what his purpose was. If the ODBC configuration is set up on the desktop, it could connect to a database. He just asked if he could use js to insert a record into a database.
Jim
@Jim, well you are also right.
Braveyard
A: 
  1. No you can't simply do that.
  2. Javascript was created for Client Side which means it can be effective at Client Side.
  3. You can use HTTP Scripting technologies to exchange data between Server side and Client Side, for example Ajax would be nice practice.

Javascript can post data to server but there must be a back-end that can evaluate the data posted by Javascript.

I don't know but maybe later they will be such a technique or tech.

Braveyard
A: 

Yes, it would be possible to create the ActiveX objects that could connect to the database and execute a query.

No, I won't post an example. Putting the code in Javascript means that anyone can just view the code and get the connection string, getting access to view and edit your database. Let's just say that the TTL for the database would get really short...

Guffa