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


AJAX sendlike.php like and delike PHP and mysql

我有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"。 而沙子.php已经改为不喜欢。

我无法执行发送<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>'的 AJAX 脚本

这是我来的远方

<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="item_$number_clickable"的东西?

提前感谢!

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

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