0

我需要帮助将这个$id值放入下面的 javascript

PHP:

<?php

$id = NULL;
$username = 'YouTube';

$xml = simplexml_load_file(sprintf('http://gdata.youtube.com/feeds/base/users/%s/uploads?alt=rss&v=2&orderby=published', $username));

if ( ! empty($xml->channel->item[0]->link) )
{
  parse_str(parse_url($xml->channel->item[0]->link, PHP_URL_QUERY), $url_query);

  if ( ! empty($url_query['v']) )
    $id = $url_query['v'];
}

echo $id; // Outputs the video ID.
    ?>

JS:需要$id值 ---> '我需要将值放在此处'

<script type="text/javascript">
   $('document').ready(function() {
    var options = { videoId: 'I need the value to go right here', start: 3 };
    $('#video1').tubular(options);
});
</script>
4

3 回答 3

2

做这样的事情......

<script type="text/javascript">
   $('document').ready(function() {
    var options = { videoId: '<?php echo $id?>', start: 3 };
    $('#video1').tubular(options);
});
</script>

但请确保您将此脚本包含在 php 文件中。

其他解决方案可能是使用 html 隐藏变量,并使用 js 访问该值。

于 2012-10-10T09:34:03.530 回答
1

通过使用

<script type="text/javascript">
  ....
</script>

我了解您在视图中使用 javascript!那么为什么不这样做呢

var options = { videoId: '<?php echo $id; ?>', start: 3 };
于 2012-10-10T09:33:47.753 回答
0

利用

<script type="text/javascript">
 var id= '<?= $id; ?>';
</script>

现在您可以在 javascript 中使用 id 变量

<script type="text/javascript">
   $('document').ready(function() {
    var options = { videoId: id, start: 3 }; // id variable which is filled by $ib variable of php
    $('#video1').tubular(options);
});
</script>
于 2012-10-10T09:37:43.827 回答