Fahad Almehaini
Fahad Almehaini

Reputation: 233

how to restrict the user not to post again on the same page if already posted

I have the below code for User Rating & Comment system which is working fine, but user can post and rate again and again.

I want that if a user posted comment already he/she should not see the comment box but a message that " You have already posted comment on this page".

I tried by using the query in the Add-Comment.php but did not worked.

Need help to solve this issue. Thanks

URL: index.php?id=1

Add-Comment.php

<?php
session_start();
    $ipaddress = $_SERVER["REMOTE_ADDR"];
    $users     = session_id();

if(!empty($_POST)){
extract($_POST);
if($_POST['act'] == 'add-com'):
    $comment = htmlentities($comment);
    $rating = htmlentities($rating);

    // Connect to the database
    require_once '../../inc/db.php';

    $default = "mm";
    $size = 35;
    $grav_url = "http://www.gravatar.com/avatar/" . "?d=" . $default . "&s=" . $size;

$sql = "INSERT INTO rest_rating (rate, comment, sr_id, ip, user)
VALUES ('$rating', '$comment', '$id_post', '$ipaddress', '$users')";

$sqls = "select user from rest_rating
where sr_id = '$id_post' and user ='$users' )";
$tt =   $db->query($sqls);

    if ( $tt['user'] == $users ) {
    echo '<font size="3" color="red">You Have Already Rated For This Restaurant</font>';
    }elseif ( $db->query($sql)==true) {
?>

    <div class="cmt-cnt">
        <img src="<?php echo $grav_url; ?>" alt="" />
        <div class="thecom">
            <!--<h5><?php echo $name; ?></h5>-->
            <b>Rating : </b><?php echo $rating; ?>          
            <span  class="com-dt"><?php echo date('d-m-Y H:i'); ?></span>
            <br/>
            <p><?php echo $comment; ?></p>
        </div>
    </div><!-- end "cmt-cnt" -->

    <?php } ?>
<?php endif; ?>
<?php } ?>

index.php

<?php
require_once '../../inc/db.php'; 
$id=$_GET['id'];
?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="css/example.css">
<link href="css/star-rating.css" media="all" rel="stylesheet" type="text/css"/>
<script src="js/star-rating.js" type="text/javascript"></script>

<div class="container">

<h3>Comments</h3>
<?php
$id_post = $id;
?> 
<div class="cmt-container" >
<?php
session_start();
$users   = session_id();
$results = $db->query("SELECT * FROM rest_rating WHERE sr_id = $id_post");
foreach ($results as $affcom) {
$comment = $affcom['comment'];
$rating = $affcom['rate'];
$date = $affcom['date'];
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/" . "?d=" . $default . "&s=" . $size;
?>

<div class="cmt-cnt">
<div class="thecom">
<input id="input-5a" class="rating" value="<?php echo $rating; ?>" data-size="xs" data-show-clear="false" data-show-caption="false" data-readonly="true">
<span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
</div>
</div><!-- end "cmt-cnt" -->
<?php } ?>



<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input name="starrating" id="starrating" value="1" type="number" class="rating" min=0 max=5 step=1 data-size="xs2" >
<textarea class="the-new-com"></textarea>

<div class="bt-add-com">Post comment</div>
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->

<?php
$sqls = "select user from rest_rating
where sr_id = '$id_post' and user ='$users' )";
$tt=$db->query($sqls);

$userT=$tt['user'];
?>
<script type="text/javascript">
   $(function(){ 
//alert(event.timeStamp);
$('.new-com-bt').click(function(event){
$(this).hide();
$('.new-com-cnt').show();
$('#name-com').focus();
});

/* when start writing the comment activate the "add" button */
$('.the-new-com').bind('input propertychange', function() {
   $(".bt-add-com").css({opacity:0.6});
   var checklength = $(this).val().length;
   if(checklength){ $(".bt-add-com").css({opacity:1}); }
});

/* on clic  on the cancel button */
$('.bt-cancel-com').click(function(){
$('.the-new-com').val('');
$('.new-com-cnt').fadeOut('fast', function(){
$('.new-com-bt').fadeIn('fast');
});
});

// on post comment click 
$('.bt-add-com').click(function(){
var theCom = $('.the-new-com');
var starrating = $('#starrating');

if( !theCom.val()){ 
alert('You need to write a comment!'); 
}else{
$.ajax({
type: "POST",
url: "add-comment.php",
data: 'act=add-com&id_post='+<?php echo $id_post; ?>+'&rating='+starrating.val()+'&comment='+theCom.val(),
success: function(html){
theCom.val('');
starrating.val('');
$('.new-com-cnt').hide('fast', function(){
$('.new-com-bt').show('fast');
$('.new-com-bt').before(html);  
})
}
});
}
});

});
</script> 

</div>

Upvotes: 0

Views: 86

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

You can construct a query to check whether the user has already posted a comment on that particular page or not, and display the rating and comment box accordingly. Here's the code snippet,

// your code

$results = $db->query("SELECT * FROM rest_rating WHERE sr_id = '". $id_post . "' AND user ='". $users . "'");
if($results->num_rows){
    // user has already posted a comment 
    echo '<p>You have already posted comment on this page</p>';
}else{
    // user hasn't posted any comment on this page yet
    // display rating and comment box
    ?>
    <div class="new-com-bt">
    <span>Write a comment ...</span>
    </div>
    <div class="new-com-cnt">
    <input name="starrating" id="starrating" value="1" type="number" class="rating" min=0 max=5 step=1 data-size="xs2" >
    <textarea class="the-new-com"></textarea>

    <div class="bt-add-com">Post comment</div>
    <div class="bt-cancel-com">Cancel</div>
    </div>
    <div class="clear"></div>
    </div><!-- end of comments container "cmt-container" -->
    <?php
}

// your code

Upvotes: 3

Related Questions