Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a MySQL query using JavaScript

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

like image 500
jardane Avatar asked Jun 27 '12 00:06

jardane


2 Answers

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

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

$.ajax({
  url: "pathToServerFile",
  type: "POST",
  data: yourParams,
  dataType: "json"
});
like image 96
Josh Mein Avatar answered Sep 19 '22 02:09

Josh Mein


You can't query with pure javascript. It has to be done from a hook that is setup on a backend.

This tends to be done with ajax.

Moreover, if querying were available from client side, then everyone could see your connection string.

like image 43
Travis J Avatar answered Sep 21 '22 02:09

Travis J