问题描述
感谢您的光临.我的物品清单很长,用户单击图像(加号)可以将该物品添加到他们的个人清单中.当他们单击+时,它会加载一个"add-item.php?itemid = *",它处理以下代码,然后将其重定向到自己的列表,我确实将其重定向回全局列表,但目前尚不清楚如果该项目已添加到用户列表中,则向用户显示.我将如何使它全部更新数据库而又不去任何地方,即时通讯使用JavaScript,但从未编写过任何内容.任何帮助将是辉煌的! :)
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!! :)
<?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'/>";
}
?>
先谢谢您! :)
推荐答案
正如大家所说,您需要AJAX.
You need AJAX, as everyone has said.
由于您从未编写过任何JavaScript,因此这里为您提供了指南.
Since you have never written any javascript, here is a guide for you.
代替您的
<a href="add-item.php?itemid='.$itemId.'" > Add Item </a>
写
<a onclick="addItemToUsersList('.$itemId.')" > Add </a>
对于AJAX,请按照Angelo的建议使用jquery.下载并添加以下内容
For AJAX, use jquery as Angelo has suggested. Download it and add the following
<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>
最后,在您的path/to/add-item.php
文件中编写代码以添加项目.参数itemId
将在此处以$_GET['itemId']
的形式提供.只需使用json_encode返回正确的状态值即可.
And then finally, in your path/to/add-item.php
file write the code to add the items. The parameter itemId
will be available here as $_GET['itemId']
. Just return proper status values using 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));
}
这篇关于如何在不重新加载页面的情况下更新mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!