How to query a database using javascript?

Another question for a beginner. I have a php variable that queries the database for a value. It is stored in the $ publish variable and its value will change (in the database) when the user clicks on the hyperlink.

if ($publish == '') { Link to publish.html } else { Link to edit.html } 

What happens in the background, I am looking at a database table for some data that is stored in the $ publish variable. If $ publish is empty, it will add a link for publish.html in the popup. The popup will process the form and add the data to the database, which means that the publication $ public is no longer empty. What I would like to achieve is that as soon as the form is processed in a popup window and the data is added to the database, the link should be changed to edit.html. This can happen when the page will repeatedly query the database, but this should happen without refreshing the page.

How can this be used with javascript, jquery or ajax? Please, help.

+8
javascript jquery ajax php
source share
8 answers

Javascript alone cannot be used to work with a database. This is done using php (or server side of your choice). Ajax is used to send a request to your php script using javascript, which in turn communicates with db. And this does not require a page refresh.

What you are trying to do can be easily achieved with ajax. Since you mentioned jquery, you can check out the $ .ajax or $ .post methods in jquery that make the process even simpler.

You need to process the form using ajax. The ajax request is sent to the php script, which will make the necessary changes to the database and send a new link (link to edit.html) in response. Once you get the answer, simply replace the current anchor with a new one.

eg.

 $.post(url, formdataobject , function (resp) { $("a.youra").text('edit').attr('href', resp); }); 

url - where is the PHP script located

formdataobject - javascript object that will have form data as key value pairs

the third parameter is an anonymous function, also known as a callback function, because it will only be called when a response is received from the server. This is because ajax requests are asynchronous.

Inside the callback function, jquery is used to change the text inside the anchor element to edit , and the href attribute is changed to the value that appeared in the response.

$. post means we are using the post method. therefore, parameters can be accessed as elements of the $ _POST array in php. After updating db, you can simply echo exit the new link and it will be received in the response.

In addition, there are other formats in which you can get an answer, for example. xml, json.

+13
source share

I will try to leave technical jargon aside and give a more general answer, since I think that you can be confused with the help of client and server scripts.

Think of javascript as a language that can only instruct your web browser how to act. Javascript is executed after the server has already completed processing your web page.

PHP, on the other hand, runs on your web server and has the ability to communicate with your database. If you want to get information from your database using javascript, you need javascript to ask PHP to query the database through an AJAX call to the PHP script.

For example, you can call a javascript script like: http://www.myserver.com/ajax_function.php?do=queryTheDatabase

In short: Javascript cannot connect to the database, but it may ask PHP to do this. Hope this helps.

+8
source share

Let me try, you want to change the link on the page from a popup that handles form processing. Try specifying a link to the container:

 <div id="publish_link"><a href="#" id="publish">Publish</a></div> 

As for submitting the form, use Ajax to send data to the server for updating and getting a response, to change the link for editing or something else:

 $.post("submit.php", { some_field: "some_value"}, function(response) { if(response.isPublished) $('#publish_link', window.opener.document).html('<a href="edit.html">Edit</a>'); }); 

Basically, your publication link is contained in a div with the publish_link identifier, so you change its content later after processing the data without reloading the page. In the pop-up window where you will process the form, this is done using the JQuery Ajax POST method to submit the data. Then your script takes this data, updates the database and, if successful, returns a response. The jQuery POST function receives this response, and there is a check there if isPublished - true, get a popup window (your main window) and update the link to Edit . Just an idea, maybe not the best of them.

+1
source share

This cannot be done using javascript, jquery or ajax. only the server side of the script can query the database. with ajax request you can get script output. Ajax requests can be sent with either pure javascript or jquery.

0
source share

Well, I think I understand your quaestion, but you should get a starting point, try to figure this out:

  • try to understand what client variables and server variables are.
  • javascript does not communicate with the database.
  • you can use javascript to retrieve data for a specific "object variable".
  • Using jquery's ajax methods, you can post this data to another page that will do the right thing.
0
source share

you can;) first you have to create a php file to query the database and return something like true or flase, and then check the function with the file url and get the answer

 function find_published(folder_id) { var aj_url = "{{server_path}}/ajax/url" var list; $.getJSON(aj_url+"?callback=?&", function(data) { //here is your data... true false ... do every thing you want } ); }; 
0
source share

this node.js application executes mysql queries https://github.com/felixge/node-mysql

0
source share

For this you need to use AJAX, for example .post() or .get() or JSON.

-2
source share

All Articles