weixin_33737134 2014-03-14 11:45 采纳率: 0%
浏览 13

使用Ajax将PHP的JQuery值转换为PHP?

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>
  • 写回答

2条回答 默认 最新

  • weixin_33691598 2014-03-14 11:51
    关注

    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);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法