将 PHP 变量传递给 jQuery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11284180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:11:20  来源:igfitidea点击:

Passing PHP Variable Into jQuery Function

phpjqueryvideopluginssrc

提问by Tom Walters

I'm trying to implement jQuery Flare Video Plugin for my website.. There's a dropdown menu which the user must choose a year from, when the submit button is clicked, a video is meant to show on the screen. I have a database that grabs the path to the video from the database i.e $row['videoName']. My question is how can I pass PHP variables in jQuery function.. In the example given in the plugin a full path to the video was given insrcattribute of jQuery function. I'm trying to make the srcdynamic by passing the PHP Variable into it.

我正在尝试为我的网站实现 jQuery Flare 视频插件。有一个下拉菜单,用户必须从中选择一年,当点击提交按钮时,视频将显示在屏幕上。我有一个数据库,它从数据库 ie 中获取视频的路径$row['videoName']。我的问题是如何在 jQuery 函数中传递 PHP 变量。在插件中给出的示例中src,jQuery 函数的属性中给出了视频的完整路径。我试图src通过将 PHP 变量传递给它来实现动态。

I'm not getting any error, and the div containing the video appears on the screen, but the video does not show.

我没有收到任何错误,包含视频的 div 出现在屏幕上,但视频没有显示。

Thank you.

谢谢你。

    jQuery(function($){
          fv = $("#video").flareVideo();
          fv.load([
            {
              src:  '$row['videoName']',
              type: 'video/mp4'
            }
          ]);
        })
      </script>

回答by Tom Walters

To access the PHP variable you must enclose the code in PHP brackets like so:

要访问 PHP 变量,您必须将代码括在 PHP 方括号中,如下所示:

jQuery(function($){
    fv = $("#video").flareVideo();
    fv.load([
      {
        src:  "<?php echo $row['videoName']; ?>",
        type: 'video/mp4'
      }
    ]);
  })
</script>

This must also be on the same page as the PHP variable is created to allow access.

这也必须与创建 PHP 变量以允许访问在同一页面上。

回答by Yarek T

I would advice to keep PHP preprocessing out of javascript as much as possible. I have a convention of creating a hash of all variables from PHP in the view and then injecting them into my Javascript objects. In this case you could put something like this into the view:

我建议尽可能将 PHP 预处理排除在 javascript 之外。我有一个约定,即在视图中从 PHP 创建所有变量的散列,然后将它们注入到我的 Javascript 对象中。在这种情况下,您可以将这样的内容放入视图中:

<script>
var options = {
    videoName: '<?php echo $row['videoName']?>'
}
</script>

or

或者

<script>
var options = <?php echo json_encode($row);?>;
</script>

Later in any of your javascript files you could do this:

稍后在您的任何 javascript 文件中,您都可以执行以下操作:

$(function(){
    fv = $("#video").flareVideo();
    fv.load([{
        src:  options.videoName,
        type: 'video/mp4'
    }]);
})

回答by Sam

The URL to the Video should be somewhere within the HTML Scope. JS comes in handy to grab the URL, with something like

视频的 URL 应该在 HTML 范围内的某个地方。JS 可以派上用场来抓取 URL,比如

fv.load({
  src: $('.videlink').attr('href'),
  type: 'video/mp4'
})

I do not know the precise javascript of this flareVideo() thing, but the URL SHOULD really be somewhere inside your HTML. Do not just pass this to the JavaScript, this is really ugly design :\

我不知道这个flareVideo() 东西的精确javascript,但URL 应该真的在你的HTML 中的某个地方。不要只是将它传递给 JavaScript,这真的是丑陋的设计:\

回答by Rich Cloutier

Another way to pass PHP variables to jQuery is through the DOM. You said that you have a dropdown list of years that the user selects. When you build your page, get the whole array of videos like so:

另一种将 PHP 变量传递给 jQuery 的方法是通过 DOM。你说你有一个用户选择的年份下拉列表。构建页面时,获取整个视频数组,如下所示:

$rows = array(
    '1991' => '/url/to/your/1991-video',
    '1992' => '/url/to/your/1992-video',
    '1993' => '/url/to/your/1993-video',
    '1994' => '/url/to/your/1994-video'
);

So you can just build your select list like so:

所以你可以像这样构建你的选择列表:

<select id="videoName">
  <option value="<?php echo $rows['1991'] ?>">1991</option>
  <option value="<?php echo $rows['1992'] ?>">1992</option>
  <option value="<?php echo $rows['1993'] ?>">1993</option>
  <option value="<?php echo $rows['1994'] ?>">1994</option>
</select>

I've used a simple array but you would use the results of your database query, and you could also use a foreach to build your drop down list.

我使用了一个简单的数组,但您将使用数据库查询的结果,您也可以使用 foreach 来构建下拉列表。

Then your video script would just reference the $('#videoName').value().

然后您的视频脚本将只引用 $('#videoName').value()。

By doing a .change() event handler on the select, you can start the video without having to reload any pages.

通过在选择上执行 .change() 事件处理程序,您可以启动视频而无需重新加载任何页面。

You can use the same approach to build tables of items based on a database query. Just name your objects or id them with unique values based on your database output.

您可以使用相同的方法基于数​​据库查询构建项目表。只需根据您的数据库输出命名您的对象或使用唯一值标识它们。

(code is untested)

(代码未经测试)

回答by disjunction

jQuery(function($){
      fv = $("#video").flareVideo();
      fv.load([
        {
          src:  '<?= $row['videoName'] ?>',
          type: 'video/mp4'
        }
      ]);
    })
  </script>

回答by Igor Parra

Mix php and js code is ugly. So when you have all your js code into .js files you can do it in this way:

混合 php 和 js 代码是丑陋的。因此,当您将所有 js 代码放入 .js 文件时,您可以通过以下方式进行操作:

code into .js files

将代码写入 .js 文件

jQuery(document).ready(function($){
    fv = $("#video").flareVideo();
    fv.load([
    {
        src:  videoName, // videoName is in the global scope
        type: 'video/mp4'
    }
    ]);
})

var videoName = ""; // init var to avoid undefined values

code into .php files

将代码写入 .php 文件

echo <<<EOM
<script type="text/javascript">
var videoName = '{$row['videoName']}';
</script>
EOM;

回答by Daniel Lefebvre

Thoughts about doing this with a cookie? I think something like this...

想用饼干来做这个吗?我认为这样的事情......

PHP

PHP

setcookie('your_cookie_name', json_encode($your_array), time()+3600, "\");

Javascript

Javascript

You would then have the PHP array in JS to do whatever JS you wanted to preform on it.

然后,您将在 JS 中使用 PHP 数组来执行您想要在其上执行的任何 JS。

 var arrayVar = []
    arrayVar = $.parseJSON($.cookie('your_cookie_name'));