Inserting Record into database using Javascript! - javascript

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.

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.

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.

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.

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.

No you can't simply do that.
Javascript was created for Client Side which means it can be effective at Client Side.
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.

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...

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.

Related

Auto-complete search, is there a way to code auto complete search without using php?

I only plan to use html, mysql, javascript and jquery.
I know learning php will be a great help in the future but it will take me too long to learn everything in a week.
For the sever side I use servlets..
Thank you
As far as I know, no. Well, strictly speaking, you don't need PHP, but you will need some server-side technology.
Assuming the data you need to autocomplete exists in mySQL, and you use jQuery.ajax() or similar to get the information on the fly, you still need some data access layer in between, which is where PHP or similar comes in. Of course, if you're accessing data that has been exposed by an API, you might have better luck.
MySQL runs on your server, but the browser can't open a socket directly to it to do queries. See: How to Use Sockets in JavaScript\HTML?
So you will need something on the server that talks to MySQL and responds back to your jQuery code. I'm all with you that you don't want to learn PHP. If you already know javascript, you can use node.js on the server. It has libraries to talk to MySQL: MySQL with Node.js
You won't be able to connect to your mySQL database directly using Javascript running on the client. The only possible way I can imagine is using another database system which offers an HTTP-interface (such as Apache Cassandra) or using a plugin for mySQL which does this job (I could not find an officially supported one, maybe you got more luck). This way, you could query data using Javascript/Ajax directly from your client.
But keep attention to security concerns, such as SQL injections or handling of client connections, which gets much more complex if you are not using a middle tier (PHP, nodejs, Java, etc.) running on a web server.
You have to use any server side technology for autocomplete. Without using any server side technology you can't create autocomplete (you can do it by adding some dummy records)
I found third party plug ins to do this
devbrige,
intellesense,
jquery auto complete
http://www.techyari.in/2014/04/jquery-autocomplete-ajax-tutorial-j2ee-json-mysql.html
https://github.com/devbridge/jQuery-Autocomplete
though I am still trying to work the codes out

How to connect to database in JavaScript

How can I connect to mysql DB from JS function?
I found this question:
How to connect to SQL server database from javascript?
but it doesn't work on Chrome, because they're using ActiveXObject.
Thank you.
There is no good solution on web browser side which will allow you to work with all browsers. There are many disadvantages to work with database on browser site.
First of all, you show your database structure and it is very dangerous. Imagine, how easier is to make SQL-injection while you know tables and fields?
You have to establish connection in some way using password which will be shown to third party users. Or you have to set password-less connection, which is also dangerous.
When you establish connection with database, somebody can easly execute own query what is trivial, because you are showing your structure.
I strongly recommended you to not do it on browser side.
but I have cross-domain problem, so I just need to do simple select from db
Direct browser-to-database communication is definitely not a proper solution.
If you want to get a list of values out of the db, just write a method in whatever server-side language you prefer (or need) to use, make the client-side JavaScript code call that method through its public URI and parse the response body into some data structure.
A more general solution to this kind of problems is XMLRPC (for the record, I've used it under Code Igniter / Flash ActionScript 3.0), though it's not all that simple to use.
If you need to get data from two different domains, then implement the above on both of them and make the JavaScript code call the two different URIs and combine the data (if needed).
You must use AJAX, because JavaScript itself can't connect to server. You can call some PHP script with AJAX and in JavaScript handle response from it. See jQuery.ajax().
make Ajax call from your javascript to php which connects you to database
Without some sort of plugin, or sending requests to a server side application that will access the database for you, you can't. So focus on those instead, specially server side apps.
A comment under this question
How to make a database connection
suggests that's only possible with a particular mix of Microsoft technologies specifically designed to deploy desktop-like software using a web browser. The bits mentioned there are:
HTA - HTML Applications
JScript - a Microsoft flavor of JavaScript
ActiveX objects
A link is also provided: Introduction to HTML Applications (HTAs)

how to access/insert into server database using html5 and javascript

I have to develop a website for mobile phones and I am using html5 and javascript. Can I access/insert into server database using html5 and javascript only without using asp, php or any other server side language.
Yes it is. You will however need a backend database with a HTTP interface. One of which is MongoDB which has a REST interface.
More info here http://www.mongodb.org/display/DOCS/Mongo+Extended+JSON
No, you can't, and if you could then you would be insane to do so as it would allow everybody to make arbitrary SQL queries.
Html and Javascript are the Front end side languages, it not advisable to use those languages features to connect to database. Later it would be very tough to maintain it and enhance it.
ALWAYS THINK IN FUTURE PERSPECTIVE, else your code becomes obsolete
instead you can use any scripting language like php to connect to database.
Since you wanted to create a website, obviously PHP and mysal will be always available to you at NO COST.
Use javascript and maybe you may not want to connect to mysql... Try .csv formats and excel... It's good if you are not storing sensitive information.

Trustable communication between your server and the browser?

I’ve always been having this problem in mind and always went with the easier more solution of doing it on the server. However, I decided I can ask more people about it, may be one of the enlightened can help me with a reliable solution.
Problem: you’re developing a web application that servers many users. Part of the features you offer involves calling an external API. That call is done for each user. The call can be made by either your server or the browser’s JavaScript. In either cases you persist the result of processing the data from the API call in the server’s database. I would like to offload calling the API and processing the results to the browser’s JavaScript and after it finishes it will callback the server with the data to persist. The problem that I see with this approach is that anyone can modify that JavaScript’s behavior (how easy is that thnx to firebug and its likes) to persist malicious/incorrect data on the server.
How can I - the server - trust that the data coming to me from JavaScript - following the previous scenario - is correct and not altered?
The simple answer is you can't, JavaScript is the least secure mechanism in the pipeline - the easiest to manipulate. If you want it to be secure, you should never rely on JavaScript for it.
Think of it in a more general sense: you can only secure an environment you at least somewhat control...you have no control over the browser, the JavaScript engine, or the user manipulating it.
Always validate server-side, always, always, always.
If you want to create some data on server A, give it to a client, and have that client pass it to server B verbatim, then you simply need to include an anti-tamper hash with it. Server A and B share a secret which they use as salt. As the client doesn't know this salt, it is unable to fabricate authentic data for itself.
Note that on its own, this technique only gives a strong degree of confidence that server A originated the data. There are other vulnerabilities you may need to consider, such as replay attacks of old data etc.

Do I need server-end knowledge (e.g. Django, Rails), if I want to do Javascript, AJAX stuff?

I am trying to get into web development, specially interested building the front-end, UI part of websites while learning JavaScript maybe with AJAX technology. (I have a UI, HCI background.)
However, I have absolutely no previous knowledge about server-end web development either. To my understanding, frameworks like Django seem to pretty good at this (correct me if I am misunderstanding).
So the question is: how much Django, or Rails do I need to know, if my interest is primarily the user interface part of web development. Can I just let somebody else do the back-end stuff?
Pardon me for my imprecise choice of terminologies.
You need to know a bit about the server side. Here's what you need to know.
If you have a heavy JavaScript website, you're likely going to want to pass information from the server to clients with JSON (JavaScript Object Notation). This is just a way to format data into strings that JavaScript knows how to convert to objects.
So, each of your server-side functions that send data to the client will return JSON. If you have someone writing the server-side for you, that's all you should have to know. You're JS functions will receive JSON, and then you deal with it.
If you have to write the server-side yourself, then that involves 1) getting data from database 2) formatting the data 3) converting to JSON.
I have open-sourced a commenting widget that accepts JSON messages, and gives examples of how you would set up the Django server code. Maybe it will help you: http://www.trailbehind.com/comment_widget/
You can make a career of front-end user interface development without know a ton about server code. You would do well though to have at least a rudimentary understanding of what happens on the server when you send it a request, where your data comes from, and what the life-cycle of a web page is. This assumes that you have the support of back-end developers. As you mentioned Ajax in your question that implies that you want your web sites to actually do something, which will require things to happen on the back-end (such as storage, manipulation of data, logging in a user, etc.).
As with all things, the more you know, the easier it will be to get what you want from the dedicated professionals. I would suggest that you learn about programming in general, not try an learn a language and framework. In particular, try to understand datatypes, server settings (like timeouts, post versus get, etc.), security and database interactions as they exist beyond JavaScript/ECMAScript. That way when a developer is explaining why they cannot do something you have requested or are offering alternatives, you are speaking the same language.
Yes and no. Typically what people think of AJAX, such as posting a comment on YouTube and seeing the comment appear instantly with a thank you message, for example, requires a server side language handling the requests, looking up data and returning results as html snippets, JSON data, or XML.
However, an AJAX call can be made to static resources as well. You could have an XML file or html snippet stored statically on your web server and have it loaded. The uses for this sort of static loading are generally fewer because if you already have the static html or data in file next to your regular page, why not just put that data directly into the page?
It helps to set up a local server and write a few lines of code to service your AJAX calls. You can do a lot of JavaScript learning with just a little back-end learning.
If you're new in web development you'd rather wait with Ajax and server-side languages until you've learnt the basics with HTML, CSS and JavaScript, especially if you want to mostly work with the user interface and not the funcionality.
As you said you can let somebody else do the back-end and focus on front-end (JavaScript, HTML, CSS).
You would need to communicate with the back-end developer when storing or processing data from the server.
As mentioned before back-end development knowledge would be useful but if you have someone doing it, it's not essential for beginning.

Categories