简体   繁体   English

检查用户是否以JQuery等级登录并警告您是否必须登录

[英]check if user is logged in with JQuery rating and alert you have to be logged in if not

Check if the user is logged in or not, If the user is logged in alert with thanks for rating, and if not alert with please log in to rate 检查用户是否已登录,如果用户已登录并显示警告以感谢评分,如果用户未登录,请登录以评分

<script>
$(document).ready(function () {
   $("#demo2 .stars").click(function ()  {

        $.post( "rate.php",{rate:$(this).val(), vid_id : 
       $vid_result['vid_id']}, function(d) {
        console.log(d);
        //JSON.parse(d);

         if(d.status && d.trim(status) == 'active'){

          alert('Thanks For Rating');

          }
          else if(d.status && d.trim(status) == 'inactive'){

          alert('You have to be logged in to rate'); 

         }

        },"json");
      });
    });
 </script>

This query uses this php bellow in order to alert the user that he/she has successfully rated. 此查询使用此PHP波纹管以提醒用户他/她已成功评分。 I have managed to insert rating only if the user is logged in with PHP and not insert rating if he/she is not logged in. However how can i fix the JQuery to alert the user with the appropriate message based on if the user is logged in or not? 我设法仅在用户使用PHP登录时才插入评分,而在他/她未登录时不插入评分。但是,如何根据用户是否登录来修复JQuery以用适当的消息提醒用户在或没有?

 // this page is rate.php

 <?php

  include 'core/init.php'; 

 if (isset($_POST['rate']) && !empty($_POST['rate'])) {
  if(isset($session_user_id)){
  $rate = sanitize($_POST['rate']);
   $vidid = sanitize($_POST['vid_id']);
 // check if user has already rated
   $sql = "SELECT `id` FROM `rating` WHERE `rater_id`='" . 
  $session_user_id."' AND  `video_id` = '".$vidid."'";
  $result = $conn->query($sql);
  $row = $result->fetch_assoc();
  if ($result->num_rows > 0) {

     $sql = "UPDATE rating SET `rate` = '$rate' WHERE `rater_id`='" . $session_user_id . "' AND `video_id` = '".$vidid."'";
      if (mysqli_query($conn, $sql)) {        }
    echo $row['id'];
} else {

    $sql = "INSERT INTO `rating` (`video_id`, `rater_id`, `rate`) VALUES ('$vidid' ,'$session_user_id', '$rate')";
    if (mysqli_query($conn, $sql)) {
     //success
    json_encode(array('status'=>'active'));
    }else
    echo "fail";
}
}else{
     //not logged in
     json_encode(array('status'=>'inactive'));
  }
  }

?>

rate.php: rate.php:

echo the json encoded array at both places 在两个地方回显json编码的数组

echo json_encode(array('status'=>'active'));

in jquery post: remove d.trim(status) instead do 在jQuery的职位:删除d.trim(status)相反

d.status && d.status == 'active'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM