I'm trying to implement a feature for reporting a comment on a site. I'm using PDO and have the function in a PHP class.
When clicking 'report' a JS function is called that uses Ajax to then call the PHP function that'll update the database / email the admin.
I can't see why but the id getting passed to the JS is always the same.
I've done various outputs to test and within the HTML the id is correct. There are currently 3 different ones that I'm testing. When I alert the id in the function it is always the same.
Any help is much appreciated.
The HTML:
<? foreach ($comments as $c){
$commentId = $c['id']; ?>
<p><a href="#" id="report" name="report" data-value="<?php echo $commentId ?>" onclick="reportComment(); return false;">Report?</a></p>
<? } ?>
The JS:
function reportComment(){
var id = $('#report').attr('data-value');
var url = "/Perspect/commentsAjax/report.php";
var params = {id: id};
$.ajax({
cache: false,
type: 'POST',
url: url,
data:params,
dataType:'json',
success: function(){
alert("sucess");
//change ahref to say this comment has been reported
},
error: function(error){
console.log(error);
}
});
alert("ID" + id);
}
PHP:
<?php include '../core/init.php';
if($_POST){
$id = $_POST['id'];
$articleComments->reportComment($id);
}
?>
.on,.clickand similar methods. But yeah, IDs have to be unique. Use a class name.