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

双心

我的网站上有一个带有随机播放列表和评论列表的视频播放器。

载入所有已写的注释。现在,我想在每次启动新视频时更改评论ID,以便站点仅显示该视频的评论。

Player是用Javascript设置的,并具有on Ready Function,它会启动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>
侯赛因·巴巴尔(Huseyin BABAL)

不要发送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);
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Ajax将JavaScript变量转换为PHP无效

来自分类Dev

使用 JOIN 时将 MySQL 值转换为 PHP 变量

来自分类Dev

php将数组转换为简单值

来自分类Dev

将JQuery RegEx转换为PHP RegEX

来自分类Dev

将 PHP 数组转换为 JQuery 数据

来自分类Dev

使用PHP将CSV转换为JSON

来自分类Dev

使用php将Word转换为html

来自分类Dev

使用PHP将Excel转换为MySQL

来自分类Dev

使用PHP将JSON转换为MySql

来自分类Dev

使用dompdf将php转换为pdf

来自分类Dev

使用PHP将JSON转换为XML

来自分类Dev

使用PHP将数组转换为json

来自分类Dev

使用PHP将JSON转换为JavaScript

来自分类Dev

使用dompdf将php转换为pdf

来自分类Dev

使用PHP将Excel转换为pdf

来自分类Dev

将文本区域转换为文件(HTML JQuery AJAX PHP)

来自分类Dev

通过AJAX将JavaScript数组转换为PHP

来自分类Dev

将传入的ajax数组转换为多维php数组

来自分类Dev

如何使用PHP将输入类型的值转换为十进制

来自分类Dev

如何使用PHP将文本输入中的值转换为Google Visualization API的数组?

来自分类Dev

使用PHP将字符串值转换为数组并获取值

来自分类Dev

将php函数返回值转换为true / false

来自分类Dev

PHP:将逗号分隔的值转换为整数数组

来自分类Dev

PHP将数组的Puttin值转换为多维数组

来自分类Dev

PHP将值从多维数组转换为数组

来自分类Dev

将php数字值转换为带有颜色的单词

来自分类Dev

PHP:将逗号分隔的值转换为整数数组

来自分类Dev

如何将选定的选项值转换为PHP

来自分类Dev

php - 将 Wordpress sql 值转换为纯文本