javascript 如何在不重新加载页面的情况下更新mysql数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3630042/
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 update a mysql database without reloading page
提问by Dan
thanks for looking. I have a very long list of items, users click on an image (a plus sign) to add the item to their personal list. At the moment when they click the + it loads a "add-item.php?itemid=*" which processes the below code then redirects them to their own list, I did have it redirecting back to the global list but then it was unclear to the user if the item was added to their list. how would I go about making it all update the database without going anywhere, im thinking javascript but have never written any. Any help would be brilliant!! :)
谢谢你看。我有一个很长的项目列表,用户单击图像(加号)将项目添加到他们的个人列表中。在他们点击 + 的那一刻,它加载了一个“add-item.php?itemid=*”,它处理下面的代码,然后将它们重定向到他们自己的列表,我确实让它重定向回全局列表,但当时不清楚如果项目已添加到他们的列表中,则向用户发送。我将如何让它在不去任何地方的情况下全部更新数据库,我在想 javascript 但从未写过任何内容。任何帮助都会很棒!!:)
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$bucketlist=MYSQL_QUERY( "SELECT * FROM membersbuckets where userid = $userid AND bucketid = $bucketid")
or die(mysql_error());
$bucketlist=mysql_fetch_array( $bucketlist ) ;
if($bucketlist < 1) {
mysql_query("INSERT INTO membersbuckets (memberbucketid, userid, bucketid, complete)
VALUES ('', '$userid', '$bucketid', '0')");
echo "Adding item to your bucketlist...";
echo "<meta http-equiv='Refresh' content='2; URL=mybucketlist.php'/>";
}
else {
echo "This item is already on your list, redirecting you to your list";
echo "<meta http-equiv='Refresh' content='2; URL=mybucketlist.php'/>";
}
?>
Thank you in advance! :)
先感谢您!:)
回答by Sandeepan Nath
You need AJAX, as everyone has said.
正如大家所说,您需要 AJAX。
Since you have never written any javascript, here is a guide for you.
由于您从未编写过任何 javascript,这里为您提供指南。
Instead of your
而不是你的
<a href="add-item.php?itemid='.$itemId.'" > Add Item </a>
Write
写
<a onclick="addItemToUsersList('.$itemId.')" > Add </a>
For AJAX, use jquery as Angelo has suggested. Download it and add the following
对于 AJAX,请按照 Angelo 的建议使用 jquery。下载并添加以下内容
<script type="text/javascript" src="http://path/to/jquery-latest.min.js"></script>
<script type="text/javasript">
function addItemToUsersList(itemId)
{
$.ajax({
'url': 'path/to/add-item.php',
'type': 'GET',
'dataType': 'json',
'data': {itemid: itemId},
'success': function(data)
{
if(data.status)
{
if(data.added)
{
$("span#success"+itemId).attr("innerHTML","Item added to your personal list");
}
else
{
$("span#success"+itemId).attr("innerHTML","This item is already on your list");
}
}
},
'beforeSend': function()
{
$("span#success"+itemId).attr("innerHTML","Adding item to your bucketlist...");
},
'error': function(data)
{
// this is what happens if the request fails.
$("span#success"+itemId).attr("innerHTML","An error occureed");
}
});
}
</script>
And then finally, in your path/to/add-item.phpfile write the code to add the items. The parameter itemIdwill be available here as $_GET['itemId']. Just return proper status values using json_encode.
最后,在您的path/to/add-item.php文件中编写代码以添加项目。该参数itemId将在此处作为$_GET['itemId']. 只需使用 json_encode 返回正确的状态值。
if($bucketlist < 1)
{
mysql_query("INSERT INTO membersbuckets (memberbucketid, userid, bucketid, complete) VALUES ('', '$userid', '$_GET['itemId]', '0')");
return json_encode(array("status" => true, "added" => true));
}
else
{
return json_encode(array("status" => true, "added" => false));
}
回答by Angelo R.
You would achieve this via JavaScript utilizing something referred to as "AJAX". An Asynchronous JavaScript And XML request allows JavaScript to send a request to another page and get the results from it.
您可以通过 JavaScript 使用称为“AJAX”的东西来实现这一点。异步 JavaScript 和 XML 请求允许 JavaScript 向另一个页面发送请求并从中获取结果。
So utilizing ajax, you would go to the URL you wanted, and then you could display the message to a user.
因此,使用 ajax,您可以转到所需的 URL,然后可以向用户显示消息。
I use a library called jQuery to get something like this done
我使用一个名为 jQuery 的库来完成这样的事情
$.ajax({
'url': 'path/to/add-item.php',
'type': 'GET',
'dataType': 'json',
'data': {itemid: xx},
'success': function(data) {
// what happens if the request was completed properly
},
'error': function(data) {
// what happens if the request fails.
}
});
Note that just because a request completes properly, doesn't mean the item was added as necessary.
请注意,仅仅因为请求正确完成,并不意味着该项目是根据需要添加的。
I would suggest you read up on the following to get a good understanding of how exactly to adapt this to your needs.
我建议您阅读以下内容,以更好地了解如何根据您的需求进行调整。
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.ajax/
回答by Nathan Hess
http://jquery.com/would be my suggestions. You are going to need to do some AJAX calls to the server to have it interact with your DB and then get a result back and display that information to the user. I prefer JQuery as it simplifies a lot of these calls and the documentation for it is fairly good. There are tons of tutorials around for the basics.
http://jquery.com/将是我的建议。您将需要对服务器进行一些 AJAX 调用以使其与您的数据库交互,然后返回结果并将该信息显示给用户。我更喜欢 JQuery,因为它简化了很多这些调用,而且它的文档相当不错。有大量的基础教程。
A simple google search reveals quite a few http://www.google.com/search?q=jquery+ajax+tutorial
一个简单的谷歌搜索揭示了很多http://www.google.com/search?q=jquery+ajax+tutorial
回答by Noddy Cha
You may really want to use Javascript in your project! See, because you are telling that you dont want to navigate out of the current page and want to give a small message on the current page saying a confirmation to the user.. then u need to use something called as AJAX-Asynchronous Javascript and XML. Its a addition of PHP and Javascript.
您可能真的想在您的项目中使用 Javascript!看,因为你告诉你不想导航出当前页面并想在当前页面上给出一条小消息向用户确认......然后你需要使用称为 AJAX-Asynchronous Javascript 和 XML 的东西. 它添加了 PHP 和 Javascript。
Using AJAX u can update currently loaded page without refreshing it. Here HTML is not connecting or requesting the server but its the Javascript. Hence u can achieve -> [how would I go about making it all update the database without going anywhere]..
使用 AJAX,您可以更新当前加载的页面而无需刷新它。这里的 HTML 不是连接或请求服务器,而是它的 Javascript。因此你可以实现 -> [我将如何让它在不去任何地方的情况下全部更新数据库]..
And as far AJAX and Javascript is concerned, they r very simple! Just need to learn basic syntax of them and den on u can go further!!!
就 AJAX 和 Javascript 而言,它们非常简单!只需要学习它们的基本语法,你就可以走得更远!!!
references u can refer to:
参考资料你可以参考:
-> Best book for u to refer is -
-> 最适合你参考的书是 -
Professional Ajax Nicholas C. Zakas, Jeremy McPeak, Joe Fawcett
职业阿贾克斯 Nicholas C. Zakas、Jeremy McPeak、Joe Fawcett

