Javascript 如何使用 jQuery 运行 MySQL 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8598659/
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 can I use jQuery to run MySQL queries?
提问by n0pe
Is it possible to run a MySQL query using jQuery? I'm trying to emulate the functionality of voting on SE sites.
是否可以使用 jQuery 运行 MySQL 查询?我正在尝试模拟在 SE 网站上投票的功能。
The vote counter on SE automatically updates without the need to reload the page (which is what I currently have, a hidden form that re-submits to the current page but runs a small block on PHP that updates the score of a question in the database). I'm assuming that is being done using Javascript/jQuery seeing as it is dynamic.
SE 上的投票计数器会自动更新,无需重新加载页面(这是我目前拥有的,一个隐藏的表单,重新提交到当前页面,但在 PHP 上运行一个小块,更新数据库中问题的分数)。我假设这是使用 Javascript/jQuery 完成的,因为它是动态的。
How can I do this? Is there a library which makes it easy and simple (like PHP)?
我怎样才能做到这一点?是否有一个使它变得简单易用的库(如 PHP)?
回答by Shyju
You can use ajax to call a server page (PHP / ASP /ASP.NET/JSP ) and in that server page you can execute a query.
您可以使用 ajax 调用服务器页面 (PHP/ASP/ASP.NET/JSP),然后在该服务器页面中您可以执行查询。
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
HTML
HTML
<input type='button' id='btnVote' value='Vote' />
Javascript
Javascript
This code will be excuted when user clicks on the button with the id "btnVote". The below script is making use of the "ajax" function written in the jquery library.It will send a request to the page mentioned as the value of "url" property (ajaxserverpage.aspx). In this example, i am sending a querystring value 5 for the key called "answer".
当用户点击 id 为“btnVote”的按钮时,将执行此代码。下面的脚本利用了 jquery 库中编写的“ajax”函数。它将向提到的页面发送请求,作为“url”属性的值(ajaxserverpage.aspx)。在这个例子中,我为名为“answer”的键发送一个查询字符串值 5。
$("#btnVote").click(function(){
$.ajax({
url: "ajaxserverpage.aspx?answer=5",
success: function(data){
alert(data)
}
});
});
and in your aspx page, you can read the querystring (in this example, answer=5) and build a query and execute it againist a database. You can return data back by writing a Response.Write (in asp & asp.net )/ echo in PHP. Whatever you are returning will be coming back to the variable data. If your query execution was successful, you may return a message like "Vote captured" or whatever appropriate for your application. If there was an error caught in your try-catch block, Return a message for that.
在您的 aspx 页面中,您可以读取查询字符串(在本例中,answer=5)并构建一个查询并在数据库中执行它。您可以通过在 PHP 中编写 Response.Write(在 asp 和 asp.net 中)/ echo 来返回数据。无论您返回什么,都将返回到可变数据。如果您的查询执行成功,您可能会返回一条消息,例如“已捕获投票”或任何适合您的应用程序的消息。如果在 try-catch 块中捕获到错误,请为此返回一条消息。
Make sure you properly sanitize the input before building your query. I usually group my functionalities and put those into a single file. Ex : MY Ajax page which handles user related stuff will have methods for ValidateUser, RegisterUser etc...
在构建查询之前,请确保正确清理输入。我通常将我的功能分组并将它们放入一个文件中。例如:处理用户相关内容的我的 Ajax 页面将具有 ValidateUser、RegisterUser 等方法...
EDIT: As per your comment,
编辑:根据您的评论,
jQuery support post also. Here is the format
jQuery 支持帖子也。这是格式
$.post(url, function(data) {
alert("Do whatever you want if the call completed successfully")
);
which is equivalent to
这相当于
$.ajax({
type: 'POST',
url: url,
success: function(data)
{
alert("Do whatever you want if the call completed successfully")
}
});
This should be a good reading : http://en.wikipedia.org/wiki/Same_origin_policy
这应该是一个很好的阅读:http: //en.wikipedia.org/wiki/Same_origin_policy
回答by Interrobang
It's just a few lines in your favorite language.
这只是您最喜欢的语言的几行。
Javascript
Javascript
$.post('script.php', { id: 12345 }, function(data) {
// Increment vote count, etc
});
PHP (simplified)
PHP(简化)
$id = intval($_POST['id']);
mysql_query("UPDATE votes SET num = num + 1 WHERE id = $id");
There are many different ways to accomplish this.
有许多不同的方法可以实现这一点。