How to execute MySQL query using JavaScript

I want to run a MySQL query on a command without reloading the page. I think JavaScript can do this, but I'm not sure how to do it. What I want to do is have a form with a return id field and when you fill out the form once with a return id and come back later and use that return id and it fills a lot of content for them to save time.

+4
source share
4 answers

Javascript cannot run MySQL queries; however, you can use ajax to make a server call to retrieve data. I like to use jQuery ajax () for my ajax needs.

Here is an example of how the jquery ajax () method works:

$.ajax({ url: "pathToServerFile", type: "POST", data: yourParams, dataType: "json" }); 
+4
source

You cannot request using pure javascript. This must be done with the help of the hook that is installed on the backend.

This is done using ajax.

In addition, if the request was accessible from the client side, everyone could see your connection string.

+4
source

You need to have a backend script to execute the query - JavaScript, which is a fully client-side language, has nothing to do with what happens to your MySQL server.

What you need to do is pass the parameters that you want in your request to any server language that you use through AJAX, and the script create and process the request as you wish.

DO NOT create a request in javascript and send it to the server - this is VERY unsafe, as it allows any user to fulfill any requests that they want.

+2
source

Using ajax will do the job. But you still need a server language to call ajax. Using jquery with ajax will be even faster!

 $.ajax({ type: "POST", url: "someserversidelangfile", data: "" //pass data through this variable }).done(function( msg ) { //do so }); 
+2
source

All Articles