Javascript 如何使用javascript查询数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5610333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to query database using javascript?
提问by Sam
Another question by a newbie. I have a php variable that queries the database for a value. It is stored in the variable $publish and its value will change (in the database) when a user clicks on a hyperlink.
一个新手的另一个问题。我有一个 php 变量,用于查询数据库的值。它存储在变量 $publish 中,当用户单击超链接时,它的值将更改(在数据库中)。
if ($publish == '') {
Link to publish.html
} else {
Link to edit.html
}
What is happening in the background is i am querying a database table for some data that i stored in the $publish variable. If the $publish is empty, it will add a link for publish.html in a popup. The popup will process a form and will add the data to the database and which means that the $publish is no more empty. What i would like to achieve is that as soon as the form is processed in the popup and a data has been added to the database, the link should change to edit.html. This can happen when the page will re-query the database but it should happen without page refresh.
在后台发生的事情是我正在查询数据库表中存储在 $publish 变量中的一些数据。如果 $publish 为空,它将在弹出窗口中添加一个 publish.html 的链接。弹出窗口将处理一个表单并将数据添加到数据库中,这意味着 $publish 不再是空的。我想要实现的是,只要在弹出窗口中处理表单并将数据添加到数据库中,链接就会更改为 edit.html。当页面将重新查询数据库时可能会发生这种情况,但它应该在没有页面刷新的情况下发生。
How can it be donw using javascript, jquery or ajax?? Please assist.
如何使用 javascript、jquery 或 ajax 关闭它?请协助。
回答by naiquevin
Javascript by itself cannot be used to deal with database. That is done using php (Or the server side language of your choice). Ajax is used to send a request to your php script using javascript which will in turn communicate with the db. And it doesn't require a page refresh.
Javascript 本身不能用于处理数据库。这是使用 php(或您选择的服务器端语言)完成的。Ajax 用于使用 javascript 向您的 php 脚本发送请求,该脚本又将与数据库进行通信。它不需要页面刷新。
So what you are trying to do can be easily achieved using ajax. Since you mentioned jquery, you can check out the $.ajax or $.post methods in jquery which make the process even more simple.
因此,您可以使用 ajax 轻松实现您想要做的事情。既然您提到了 jquery,您可以查看 jquery 中的 $.ajax 或 $.post 方法,它们使过程更加简单。
You need to process the form using ajax. The ajax request is sent to a php script which will make the necessary changes in the database and send the new link (link to edit.html) in the response. Upon getting the response, just replace the current anchor element with the new one ..
您需要使用ajax处理表单。ajax 请求被发送到一个 php 脚本,该脚本将在数据库中进行必要的更改并在响应中发送新链接(链接到 edit.html)。获得响应后,只需用新的锚元素替换当前的锚元素..
for eg..
例如...
$.post(url, formdataobject , function (resp) {
$("a.youra").text('edit').attr('href', resp);
});
url
- where the php script is located
url
- php 脚本所在的位置
formdataobject
- a javascript object that will have the form data as key value pairs
formdataobject
- 一个 javascript 对象,它将表单数据作为键值对
the third parameter is an anonymous function also known as callback function since it will be invoked only when the response is received from the server. This is because ajax requests are asynchronous.
第三个参数是一个匿名函数,也称为回调函数,因为只有在从服务器收到响应时才会调用它。这是因为 ajax 请求是异步的。
Inside the callback function, jquery is used to change the text inside the anchor element to edit
and the href
attribute is changed to value that came in the response.
在回调函数内部,jquery 用于将锚元素内的文本更改为edit
,并将href
属性更改为响应中的值。
$.post means we are using the post method. so the parameters can be accessed as elements of $_POST array in php.
After updating the db, you can simply echo
out the new link and it will be received in the response.
$.post 表示我们正在使用 post 方法。所以参数可以作为 $_POST 数组的元素在 php 中访问。更新数据库后,您可以简单地echo
删除新链接,它将在响应中收到。
Also, there are other formats in which you can get the response for eg. xml, json.
此外,您还可以通过其他格式获得响应,例如。xml,json。
回答by soulkphp
I'll try to leave the technical jargon aside and give a more generic response since I think you might be confused with client-side and server-side scripting.
我会尽量把技术术语放在一边,给出一个更通用的回答,因为我认为你可能会对客户端和服务器端脚本感到困惑。
Think of javascript as a language that can only instruct your WEB BROWSER how to act. Javascript executes after the server has already finished processing your web page.
将 javascript 视为一种只能指导您的 WEB 浏览器如何操作的语言。Javascript 在服务器完成处理您的网页后执行。
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'll need to have javascript ask PHP to query the database through an AJAX call to a PHP script.
另一方面,PHP 在您的 Web 服务器上运行,并且能够与您的数据库进行通信。如果您想使用 javascript 从数据库中获取信息,则需要让 javascript 要求 PHP 通过对 PHP 脚本的 AJAX 调用来查询数据库。
For example, you could have javascript call a script like: http://www.myserver.com/ajax_function.php?do=queryTheDatabase
例如,您可以让 javascript 调用如下脚本:http://www.myserver.com/ajax_function.php?do =queryTheDatabase
In summary: Javascript can't connect to the database but it can ask PHP to do so. I hope that helps.
总结:Javascript 无法连接到数据库,但它可以要求 PHP 这样做。我希望这有帮助。
回答by tradyblix
Let me try, you want to change the link in a page from a pop-up that handles a form processing. Try to give your link a container:
让我尝试一下,您想从处理表单处理的弹出窗口更改页面中的链接。尝试为您的链接提供一个容器:
<div id="publish_link"><a href="#" id="publish">Publish</a></div>
As for the form submission use Ajax to submit data to the server to do an update and get a response back to change the link to edit or something:
至于表单提交,使用 Ajax 向服务器提交数据以进行更新并获得响应以更改链接以进行编辑或其他内容:
$.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 publish link is contained in a div
with an ID publish_link
so you change its content later after data processing without reloading the page. In the pop-up where you would do the form processing it is done using jQuery Ajax POST method to submit the data. Your script then accepts that data, update the database and if successful returns a response. jQuery POST function receives that response and there's a check there if isPublished
is true, get the pop-up's opener window (your main window) and update the link to Edit
. Just an idea, may not be the best out there.
基本上,您的发布链接包含在div
带有 ID 的a 中,publish_link
因此您可以在数据处理后更改其内容,而无需重新加载页面。在您将进行表单处理的弹出窗口中,它使用 jQuery Ajax POST 方法提交数据。然后您的脚本接受该数据,更新数据库,如果成功则返回响应。jQuery POST 函数接收该响应并检查那里是否isPublished
为真,获取弹出窗口的开启窗口(您的主窗口)并将链接更新为Edit
. 只是一个想法,可能不是最好的。
回答by Mohammad Efazati
you can ;) at first you must create php file to query database and return something like true or flase and then with file url check the function and get answer
你可以 ;) 首先你必须创建 php 文件来查询数据库并返回类似 true 或 flase 的东西,然后用文件 url 检查函数并得到答案
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
}
);
};
回答by Headshota
It cannot be made with javascript, jquery or ajax. only server side script can query a database. with ajax request you can get the script output. ajax requests can be sent either with pure javascript or jquery.
它不能用 javascript、jquery 或 ajax 制作。只有服务器端脚本可以查询数据库。使用 ajax 请求,您可以获得脚本输出。ajax 请求可以使用纯 javascript 或 jquery 发送。
回答by Flavio Oliveira
Well, i think i understand your quaestion, but you have to get a starting point, try to understand this:
好吧,我想我理解你的问题,但你必须有一个起点,试着理解这一点:
- try to understand what are client variables and server variables.
- javascript does not comunicate with database.
- you can use javascript to retrieve data to a specific "Object variable".
- Using ajax methods of jquery you can post that data do other page, that will execute the proper actions
- 尝试了解什么是客户端变量和服务器变量。
- javascript 不与数据库通信。
- 您可以使用 javascript 将数据检索到特定的“对象变量”。
- 使用 jquery 的 ajax 方法,您可以将该数据发布到其他页面,这将执行适当的操作
回答by Shawn
this app for node.js does mysql queries https://github.com/felixge/node-mysql
这个用于 node.js 的应用程序执行 mysql 查询https://github.com/felixge/node-mysql
回答by json
You need to use AJAX for this, like .post()
or .get()
or JSON.
为此,您需要使用 AJAX,例如.post()
or.get()
或 JSON。