weixin_33744141 2013-11-06 04:41 采纳率: 0%
浏览 29

在AJAX中使用PHP变量

I am trying to accomplish the following on the page "videos.php" (note, this is all inside of a PHP echo):

  1. User clicks .star_' . $pvid_ID . ' which submits a video rating to a form (this is working properly)
  2. Now, I need to refresh the div that displays the current video rating to show the updated video rating, which is stored in the variable $avg_rating (grabbed from the DB in an earlier query on videos.php).
  3. To update the variable $avg_rating, I want to POST the id of the video to find_ratings2.php (that video ID is currently stored on "videos.php" as $pvid_ID)

Here is what I have as my jquery/ajax on "videos.php":

echo'
<script type="text/javascript" language="javascript">
$(document).ready(function() {
   $(".star_' . $pvid_ID . '").click(function() {
       $.ajax({
           type: "POST",
           url: \'/dev/scripts/find_ratings2.php\',
           data: { videoid: ' . $pvid_ID . ' },
           success: function(data) {
               $(".parent_video_' . $pvid_ID . '").load(\'testratings.php .vid_frame_id_' . $pvid_ID . '\').hide().fadeIn(2000);
            }
        });
    });
});                        
</script>
';

then, on find_ratings2.php I've got:

<?php
require('config.php');
require('checklogin.php');

$video_id = $_POST['videoid'];
$mysqlicon3 = mysqli_connect($db_host, $db_username, $db_password, $db_name);
$find_rating = mysqli_query($mysqlicon3, "SELECT AVG(rating) AS avgRating FROM videoRatings WHERE videoID = '$video_id'");

while ($rating_row = mysqli_fetch_array($find_rating)) {
    $avg_rating = $rating_row['avgRating'];
}
?>

When .star_' . $pvid_ID . ' is clicked, the div is being refreshed properly (the .hide() and .fadeIn() are also working), but $avg_rating is not being updated.

For reference, $avg_rating is on "videos.php" like this:

echo'
<div class="parent_video_' . $pvid_ID . '">
    <div class="comment_iconimg-stars vid_frame_id_' . $pvid_ID . '">
        <div class="video-stars"><input class="imgstar star_' . $pvid_ID . '" type="image" src="';
        if ($avg_rating > 0){echo '/dev/images/rate_video_icon_yellow.png';} else{echo '/dev/images/rate_video_icon.png';}
        echo '" border="0" /></div>
        </div>
    </div>
</div>';
  • 写回答

2条回答 默认 最新

  • weixin_33743880 2013-11-06 05:10
    关注

    While multiple problems found in your code apart from echo html/js from php as @MatRt indicate. Still one problem why $avg_rating variable not updated because $avg_rating variable takes its inital value since page load not from ajax request. You need to update this from javascript something like that.

    if(data > 0)
    { 
        $("video-stars input.imgstar").attr('src', '/dev/images/rate_video_icon_yellow.png');   
    }
    else
    {
        $("video-stars input.imgstar").attr('src','/dev/images//dev/images/rate_video_icon.png');   
    }  
    

    Also echo $avg_rating in find_ratings2.php in last.

    评论

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元