, . GET, URL POST; , POST.
: , , , , .. .
, , , . , . .
view.php
, , script. - - ...
<a class="flag" href="flag.php?comment=100">Flag</a>
view.php click, javascript- jQuery ( $.ajax() $.get $.post MooTools Prototype).
, 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) {
window.location = 'login.php';
} else if (data.flagged == 1 && data.count != -1) {
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.');
}
});
event.preventDefault();
return false;
});
});
</script>
flag.php
flag.php - , mysql-, , , PHP, JSON. (), , :
{"flagged":1,"count":15,"error":0}
( ), , 15 (, ) .
. HTML. Javascript $.ajax(). , HTML, , . . http://www.json.org/example.html.
<?php
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');
$json = array('flagged'=>0,'count'=>-1,'error'=>0);
if (!$logged_in) {
$json['error'] = -1;
exit(echo(json_encode($json)));
}
$comment = mysql_real_escape_string($_GET['comment']);
if (empty($comment) || !is_numeric($comment)) {
$json['error'] = 1;
} else {
$result = mysql_query("
UPDATE comments
SET flags = flags+1
WHERE commentID = $comment
");
if (!$result) {
$json['flagged'] = 0;
$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 {
$json['error'] = 3;
}
}
}
echo json_encode($json);
?>