[英]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中):
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.