你可以做什么的大纲。我做了一些假设,并选择了使用 URL 方法而不是 POST 的 GET;如果您更喜欢使用 POST,请根据口味进行编辑。
另一个注意事项 - 你可能想要一个标志表,以便让你关注谁标记了谁,也许让他们添加评论等......只是一个想法。
这只是为了让您了解如何按照您的要求去做。我还没有测试过代码,所以买家要小心。这只是一个起点。
view.php
您可以在此处获得最终用户单击以激活标志脚本的链接。在您的标记中的某处,您可能会有类似...
<a class="flag" href="flag.php?comment=100">Flag</a>
然后,在 view.php 内容中,您将拥有尝试 jQuery 异步 javascript 请求的点击事件代码(在这种情况下为 $.ajax(),可以是 $.get 或 $.post,或者完全类似的其他库MooTools 或原型)。
该示例演示了服务器将在 data 参数中返回 JSON 格式的文本,然后对其进行评估以检查服务器的响应内容(成功?失败?部分成功?需要登录?)。见http://json.org/。
<script type="text/javascript">
$(document).ready(function(){
$('a.flag').click(function(event){
$.ajax({
type: "GET",
url: $(this).attr('href'),
data: dataString,
dataType: "json",
success: function (data) {
if (data.error == -1) {
// Not logged in, redirect to login page.
window.location = 'login.php';
} else if (data.flagged == 1 && data.count != -1) {
// Success! With a count too.
alert('Comment flagged ('+data.count+' times flagged).');
} else {
switch(data.error) {
case 1:
alert('Comment not found');
break;
case 2:
alert('Comment not flagged due to an update error.');
break;
case 3:
alert('Comment flagged but count not returned.');
break;
default:
alert('There was a general error flagging the comment.');
break;
}
}
},
error: function(){
alert('Comment not flagged; general send error.');
}
});
// Call these to prevent the a tag from redirecting
// the browser to the a-tags href url.
event.preventDefault();
return false;
});
});
</script>
flag.php
flag.php 是允许您运行 mysql 查询的服务器页面,当然使用 PHP,然后使用一些 JSON 格式的文本进行响应。页面返回的内容(文本)如下所示:
{"flagged":1,"count":15,"error":0}
这将向您的调用页面(在浏览器中)表明该评论已被标记,已被标记 15 次(差评、差评),并且没有错误。
这很重要:这不等同于 HTML。这种类型的文本是数据,并在响应 $.ajax() 函数时被解析回 Javascript 对象。所以,不要在它周围放任何 HTML,因为那不是你应该做的。有关示例,请参阅http://www.json.org/example.html。
<?php
// Need to output JSON headers and try to prevent caching.
session_cache_limiter('nocache');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
// Our JSON array to return
// - flagged would be 1 = success, 0 = failure
// - count would return the # flags, with -1 no return #
// - error code, see comments for description
$json = array('flagged'=>0,'count'=>-1,'error'=>0);
// Your logged in check code goes here
// you don't want non-logged in people doing this
// Here, however you test to find out if someone is logged in,
// check and return a -1 error to redirect the login.
if (!$logged_in) {
// error -1 = not logged in, redirect browser
$json['error'] = -1;
exit(echo(json_encode($json)));
}
// Your mysql connection code goes here
$comment = mysql_real_escape_string($_GET['comment']);
if (empty($comment) || !is_numeric($comment)) {
// error 1 = comment id not found
$json['error'] = 1;
} else {
$result = mysql_query("
UPDATE comments
SET flags = flags+1
WHERE commentID = $comment
");
if (!$result) {
$json['flagged'] = 0;
// error 2 = update error
$json['error'] = 2;
} else {
$json['flagged'] = 1;
$count = mysql_query("
SELECT flags
FROM comments
WHERE commentID = $comment
LIMIT 0, 1
");
if ($count) {
$query = mysql_fetch_assoc($count);
$json['count'] = $query['count'];
} else {
// error 3 = updated but did not get count
$json['error'] = 3;
}
}
}
echo json_encode($json);
?>