php - AJAX sendlike.php 喜欢和不喜欢 PHP 和 mysql

标签 php jquery mysql ajax

我有 sendlike.php 来处理点赞,并且到目前为止一直有效!

<?php
//include db configuration file
include_once("config.php");
//Include functions
include_once("functions.php");

// http://stackoverflow.com/questions/21065502/ajax-a-href-onclick-get-php-variable-and-execute-php-file

// For practice
    $uid_fk = 1;

//check $_POST["content_txt"] is not empty
if(isset($_GET["like"]) && strlen($_GET["like"])>0) 
{   

    //Add IP, date. Set the Foreign key
    $ip = $_SERVER['REMOTE_ADDR'];
    $date = date("Y-m-d H:i:s");
    $comment_id_fk = $_GET["like"]; // '".$comment_id_fk."', What comment has been liked?

    // $_SESSION["user_id"]  '".$uid_fk."', What user has liked it? Get the users uid

    // Insert sanitize string in record
    $insert_row = $mysqli->query("INSERT INTO comment_likes (comment_id_fk, uid_fk,date,ip)
        VALUES('".$comment_id_fk."','".$uid_fk."','".$date."','".$ip."')");

    if($insert_row)
    {
        //Count the amount of likes again
        $count_likes=$mysqli->query("SELECT COUNT(*) as TOTAL_COMMENT_LIKES FROM `comment_likes` 
        WHERE comment_id_fk='".$comment_id_fk."'");
        $row_array=$count_likes->fetch_array(MYSQLI_ASSOC);

         //Record was successfully inserted, respond result back to index page
        // This should probably in the fute go thruh functions.php comment_func_bar
          $my_id = $mysqli->insert_id; //Get ID of last inserted row from MySQL. Will this cause problems when alot of ppl liking / posting / commenting etc??
         echo '<a href="sendlike.php?delike='.$comment_id_fk.'" class="clickable">' . $row_array['TOTAL_COMMENT_LIKES'] .' Unlike</a>';

          $mysqli->close(); //close db connection

    }else{

        //header('HTTP/1.1 500 '.mysql_error()); //display sql errors.. must not output sql errors in live mode.
        header('HTTP/1.1 500 Looks like mysql error, could not insert record!');
        exit();
    }

}
elseif(isset($_GET["delike"]) && strlen($_GET["delike"])>0 && is_numeric($_GET["delike"]))
{   //do we have a delete request? $_POST["recordToDelete"]

    //sanitize post value, PHP filter FILTER_SANITIZE_NUMBER_INT removes all characters except digits, plus and minus sign.
    $idToDelete = filter_var($_GET["delike"],FILTER_SANITIZE_NUMBER_INT); 

    //try deleting record using the record ID we received from POST
    $delete_row = $mysqli->query("DELETE FROM comment_likes WHERE comment_id_fk='".$idToDelete."' AND uid_fk ='".$uid_fk."'"); //uid_fk is $_SESSION[user_id] actually

    if(!$delete_row)
    {    
        //If mysql delete query was unsuccessful, output error 
        header('HTTP/1.1 500 Could not delete record!');
        exit();
    }
    $mysqli->close(); //close db connection
}
else
{
    //Output error
    header('HTTP/1.1 500 Error occurred, Could not process request!');
    exit();
}
?>

如果你看一下回显线,href 的类是 class="clickable"。 sandlike.php 已更改为 delike。

我无法执行 AJAX 脚本来发送 <a href="sendlike.php?like=' . $comment_id .'" class="clickable">并将“可点击”href 更新为 <a href="sendlike.php?delike='.$comment_id_fk.'" class="clickable">' . $row_array['TOTAL_COMMENT_LIKES'] .' Unlike</a>'

这就是我目前为止的情况

<script type="text/javascript">
    $('.clickable').on('click', function() {
        var data = {
            mode: "like",
            rating: $(this).data('rating'),
            id: $(this).data('id')
        };
        $.ajax({
            type: 'GET',
            url: 'sendlike.php',
            data: data,
            success: function(response) {
                console.log(response);
            }
        });
    });
</script>

而且离工作地点也不近。

那么 class="clickable"怎么样,我在页面上有多个评论。都会被喜欢/不喜欢。或者我需要类似 class="item_$number_clickable"的东西吗?

提前致谢!

最佳答案

添加event.preventDefault()并将事件委托(delegate)给静态父级:

$(document).on('click', '.clickable', function(e) {
    e.preventDefault(); // to stop the page navigation

关于php - AJAX sendlike.php 喜欢和不喜欢 PHP 和 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28986887/

相关文章:

jquery - CKeditor 实例为 null 或未定义?

mysql - 如何使用单词边界在 MYSQL REGEXP 查询中查找带有连字符的单词?

php - 根据订单状态隐藏 WooCommerce 管理订单列表中的订单(表行)

php artisan route::list 给我一个错误:试图获取非对象的属性

php - <li> 元素的 preg_match_all

php - 使用 PHP 执行 XQuery

jquery - 在MVC3中使用CheckBoxFor时,如何使用JQuery仅选择CheckBox INPUT?

jquery - Wrapper Div 故障/在页面加载时移动以居中

mysql - 在mysql数据库中触发之前

mysql - 为什么我不能在 wordpress 中使用 CREATE 直接创建表