How to talk to a database from Javascript? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want variables from my Javascript functions to be recorded in a database on my server and then posted back onto the html page. I know this may be easy with the POST and GET method, but I've been reading and I'm stumped. So if anyone could lead me to a sample that shows how to do this or show me here in an example of one that would help.

You need to use some form of server-side scripting language (such as PHP) to capture the data sent from JavaScript (presumably via means of AJAX's XMLHttpRequest object) and insert it in the database.
i.e.: There's no means of directly communicating with MySQL from JavaScript itself - you need to use a server-side scripting language to do this.

You might wanna read into Ajax which uses the XMLHttpRequest object to do exactly that.
Using this you can send data to and from a server side script. So that way you could "insert variables into database."

Related

How to send information back and forth from a javascript file to a python file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So I have an html file with javascript and a python file. In the javascript file, the user enters a string. In the python, I would like it to search it in an api, and then return the string to be displayed in the html file. How do I do this? I have looked up how to use AJAX and get/post methods, but nothing has worked so far. Also, I should mention I am using flask.
The basic mechanics of having an HTML page communicate directly with another program is web server programming. If you want the web server to execute python, then consider setting up a Django server. Learning how to set up a web server and getting the two programs to talk with each other may take a while if it is your first time.

Which is faster ..passing variables to a javascript function or running a MySql query to fetch the values? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wish to know the following: Which one of the following is faster- passing variables to a javascript function and then passing it on to a php file to carry out some operation or running a MySql query to fetch the variables in the php file without passing them? ..Thanks a lot!
For pretty similar request .. if the related value are already available to te client and you need this to some other page then tecnically
the javascript ajax request involve only the internet transfer of the call and the transfer of the result .. the use of database access in addition to sending the request and receiving data also implies the access to the database so normally should be more fast ths ajax method
Unless you have a specific reason for using Javascript, for instance, updating displayed records without reloading the page, use PHP directly. You can always run the php function in a class on a separate file if you wish to keep your html and php separate.
My personal experience of ajax, which you should remember still has to access PHP in order to execute server-side requests, is that it can appear slow. Whether or not it actually is slower, it leaves the page in place whilst executing the code and so might present that illusion to the user.
Also, I think the simplest solution is the best, so if you don't need ajax you shouldn't use it, but if you do need it then there is no reason not to do so that I am aware of.

Getting updating in codebehind C# variable to asp.net [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wrote a small code in c# that reads files from the hard drive and outputs that info to a webpage using asp.net.
Information about those files is being changed in the codebehind.
I managed to create variables to read and display the info on the webpage, but they change only after a refresh. When I used setTimeout it would always read the same variable, leaving me thinking that the codebehind does not get re-executed.
How would it be possible to have them updated live without needing to refresh the entire page?
This is somewhat vague but if you want updates to the values on your page after loading, you'll need to use some javascript to grab new values and then more javascript to update values on your page.
There are some great frameworks out there. Unless you have a lot of front-end logic you need to perform through javascript, I'd keep it as simple as possible and throw jQuery in there for the AJAX calls (fyi, this is not the only solution, there are 10+ different ways to skin this same cat).
If you want a bit more efficiency, you could look into using SignalR - which wraps long polling or web sockets (depending on browser capability) on the front end and signals those subscribing pages on the backend only when changes have occurred to data.
Link: http://www.asp.net/signalr
There is no "right" answer to your question so the best I or anybody can do is guide you in a direction. Hopefully this answer helps you.

Saving all javascript code in an online database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am really new to making an async game and I would like to know if you could just get the current gamestate and save it as it is or if I would have to grab all code seperatly and organising them into tables. It would be very useful if there was some kind of function that could save everything at once as I am building a pretty big game and it would take a long time grabbing every last piece of information.
So to summarise: I would like to grab the existing gamestate and put everything in a database, then grab everything again and restore that gamestate.
Thanks in advance!
You'll need to submit (or retrieve) the gamestate data to a server with JavaScript. You can use something like JQuery's ajax function for this.
For the server to do anything meaningful with your data, you'll need to use some kind of scripting language. PHP or Node.JS might be a good place to start if you're new to this.
Once the server gets the HTTP request, you can have it send a query to your database (like MySQL) and send back some kind of response.

Writing to a database using javascript bad idea? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I made a database that holds information about clinical trials and so far I have been accessing and writing to it using php. I was wondering if there was a way to read/write to a SQL Server database using javascript or jquery? The UI I am developing will be for adding clinical trial data to the database. Only the DB admins will have access to this UI so security should not be a "huge" problem.
You need a middle tier like php, rails, java... to do the database write. You can't do this from the browser with javascript. But there is Node.js, which allows you do write javascript on the server.
In short, no. Even if there was, you should never leave data validation to the client. Just because your DB Admins are trustworthy, doesn't mean those who break into your network will be. DROP TABLE ClinicalTrialData; would be a bad thing. Use PHP/backend of choice to do the donkeywork and use AJA[X|J] if you want a slick UI experience.
Only server side Javascript methods such as using NodeJS as your server. Never put your database credentials on the front end unless you want people to directly access your database.

Categories

Resources