我的网站有一个带有随机播放列表和评论列表的视频播放器。
所有写的评论都已加载。 现在,我想在每次新视频开始时更改注释ID,以便网站仅显示此视频的评论。
播放器在Javascript中设置,并具有一个就绪功能,可以激活ajax功能。 注释被设置为带有$ value的php行。
这是我的代码:
<div id="comments"> <?php $commentsID= 3; //Testnumber 3 shows all comments to video 3 Comment::getCommentSystem($commentsID); ?> </div> <script> onReady: function(event) { videoID; // actual videoID //and here comes some Ajax Magic, to tell $commentsID = videoID, but how? // My example doesn't work because it's my first try with Ajax whoohooo $.ajax({ type: "GET", url: "index.php", data: videoID, success: function(videoID){ $('#comments').empty(); // Clear Testnumber'n stuff $(' <?php $commentsID= videoID; Comment::getCommentSystem($commentsID); ?> ').appendTo('#comments'); // rewrite the comments Div with the videoID } }); </script>编辑:
现在我的代码看起来像这样:
<div id="comments"> </div> <script> [...] onReady: function(event) { videoID; // actual videoID $.ajax({ type: "GET", url: "get_comments.php?videoId=" + videoID, success: function(response){ $('#comments').html(response); } }); } [...] </script>get_comments.php
<?php session_start(); include "comment.class.php"; $videoID = $_GET["videoId"]; $comments = Comment::getCommentSystem($videoID); return($comments); ?>它产生了这个:
<div id="comments"> <!-- The Form to add a comment ( always display none ) --> <div style="display:none;"> <div class="comment-post comment-child"> <form id="addCommentForm" action="" method="post"> <!-- The Form container, that shows the Form comment --> <!-- ( should be visible - maybe session fail? ) --> <div class="comment-container" style="display:none;"> <div class="comment-content"> <!-- all comments to videoID 3 --> <ul class="comment-list-3"> </div>My Website has a Video Player with a random Playlist and a Comments List.
All Comments ever written are loaded. Now I want to change the comments ID, everytime a new Video starts, so that the Site shows only comments for this Video.
The Player is set up in Javascript and has an on Ready Function, that fires an ajax function. The Comments are set up as a php line with a $value.
This is my code:
<div id="comments"> <?php $commentsID= 3; //Testnumber 3 shows all comments to video 3 Comment::getCommentSystem($commentsID); ?> </div> <script> onReady: function(event) { videoID; // actual videoID //and here comes some Ajax Magic, to tell $commentsID = videoID, but how? // My example doesn't work because it's my first try with Ajax whoohooo $.ajax({ type: "GET", url: "index.php", data: videoID, success: function(videoID){ $('#comments').empty(); // Clear Testnumber'n stuff $(' <?php $commentsID= videoID; Comment::getCommentSystem($commentsID); ?> ').appendTo('#comments'); // rewrite the comments Div with the videoID } }); </script>EDIT:
Now my code looks like this:
<div id="comments"> </div> <script> [...] onReady: function(event) { videoID; // actual videoID $.ajax({ type: "GET", url: "get_comments.php?videoId=" + videoID, success: function(response){ $('#comments').html(response); } }); } [...] </script>get_comments.php
<?php session_start(); include "comment.class.php"; $videoID = $_GET["videoId"]; $comments = Comment::getCommentSystem($videoID); return($comments); ?>and it produces this:
<div id="comments"> <!-- The Form to add a comment ( always display none ) --> <div style="display:none;"> <div class="comment-post comment-child"> <form id="addCommentForm" action="" method="post"> <!-- The Form container, that shows the Form comment --> <!-- ( should be visible - maybe session fail? ) --> <div class="comment-container" style="display:none;"> <div class="comment-content"> <!-- all comments to videoID 3 --> <ul class="comment-list-3"> </div>最满意答案
不要发送它index.php,将请求发送到另一个端点,如get_comments.php ,
<script> onReady: function(event) { videoID; // actual videoID //and here comes some Ajax Magic, to tell $commentsID = videoID, but how? // My example doesn't work because it's my first try with Ajax whoohooo $.ajax({ type: "GET", url: "get_comments.php?videoId=" + videoID, success: function(response){ $('.comment-list-3').empty(); // Clear Testnumber'n stuff var html = ''; $.each(response, function(i, item) { // Do your html here. I assume, your comment object has a field "text". Update it according too your need html += '<div>' + item.text + '</div>'; }); $('.comment-list-3').html(html); // rewrite the comments Div with the videoID } }); </script>并在你的get_comments.php ;
<?php $videoID = $_GET["videoId"]; $comments = Comment::getCommentSystem($videoID); // Let say this is array echo json_encode($comments); ?>Do not send it index.php, send request to another endpoint like get_comments.php,
<script> onReady: function(event) { videoID; // actual videoID //and here comes some Ajax Magic, to tell $commentsID = videoID, but how? // My example doesn't work because it's my first try with Ajax whoohooo $.ajax({ type: "GET", url: "get_comments.php?videoId=" + videoID, success: function(response){ $('.comment-list-3').empty(); // Clear Testnumber'n stuff var html = ''; $.each(response, function(i, item) { // Do your html here. I assume, your comment object has a field "text". Update it according too your need html += '<div>' + item.text + '</div>'; }); $('.comment-list-3').html(html); // rewrite the comments Div with the videoID } }); </script>and in your get_comments.php;
<?php $videoID = $_GET["videoId"]; $comments = Comment::getCommentSystem($videoID); // Let say this is array echo json_encode($comments); ?>更多推荐
发布评论