简体   繁体   English

在AJAX中使用PHP变量

[英]Use PHP variable in AJAX

I am trying to accomplish the following on the page "videos.php" (note, this is all inside of a PHP echo): 我正在尝试在“ videos.php”页面上完成以下操作(注意,这全部都在PHP echo中):

  1. User clicks .star_' . 用户点击.star_'。 $pvid_ID . $ 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). 现在,我需要刷新显示当前视频收视率的div来显示更新的视频收视率,该更新存储在变量$ avg_rating中 (从DBs中对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 ) 要更新变量$ avg_rating ,我想将视频的ID发布到find_ratings2.php (该视频ID当前以$ pvid_ID的形式存储在“ videos.php”上)

Here is what I have as my jquery/ajax on "videos.php": 这是我在“ videos.php”上的jquery / ajax所拥有的:

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: 然后,在find_ratings2.php上,我得到了:

<?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_' . .star_'时。 $pvid_ID . $ pvid_ID。 ' is clicked, the div is being refreshed properly (the .hide() and .fadeIn() are also working), but $avg_rating is not being updated. 单击' ,将正确刷新div(.hide()和.fadeIn()也可以正常工作),但是$ avg_rating没有更新。

For reference, $avg_rating is on "videos.php" like this: 作为参考, $ avg_rating在“ videos.php”上如下所示:

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>';

While multiple problems found in your code apart from echo html/js from php as @MatRt indicate. 虽然在代码中发现了多个问题,但@MatRt表示从php回显html / js。 Still one problem why $avg_rating variable not updated because $avg_rating variable takes its inital value since page load not from ajax request. 还有一个问题,为什么$ avg_rating变量不更新,因为$ avg_rating变量取其初始值,因为页面加载不是来自ajax请求。 You need to update this from javascript something like that. 您需要使用javascript之类的工具来更新它。

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. 最后还要在find_ratings2.php中回显$ avg_rating。

Close PHP end tag and add javascript code: 关闭PHP结束标记并添加javascript代码:

<script type="text/javascript" language="javascript">
$(document).ready(function() {

   var pvid = "<?php echo $pvid_ID; ?> ";

   $(".star_' + pvid + '").click(function() {
       $.ajax({
           type: "POST",
           url: \'/dev/scripts/find_ratings2.php\',
           data: { videoid: ' + pvid +  ' },
           success: function(data) {
               $(".parent_video_' + pvid +  '").load(\'testratings.php .vid_frame_id_' . + pvid +  '\').hide().fadeIn(2000);
            }
        });  });});                        
</script>
<?php some code ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM