Javascript 是否可以将 php 变量插入到您的 js 代码中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11474684/
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-24 05:58:51  来源:igfitidea点击:

Is it possible to insert php variables into your js code?

phpjavascriptjqueryajax

提问by campatsky

Possible Duplicate:
passing variables from php to javascript

可能的重复:
将变量从 php 传递到 javascript

I'm dynamically generating a list. I want to make each row hover on mouseover and clickable to a link. I want the link to pass the id of the content of the row.

我正在动态生成一个列表。我想让每一行都悬停在鼠标悬停上并可以点击链接。我希望链接传递行内容的 id。

Basically:

基本上:

foreach ($courses as $cid=>cinfo){ 
  $univ = $cinfo['univ'];
  $desc = $cinfo['desc'];
  $size = $cinfo['size'];
  $start = $cinfo['start'];
  print "<div class='rc_desc' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
        "<span>Number of students</span>: $size<br/>".
        "<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
}

<script>
    $(function ()
    {
      $('#rc_desc$cid').hover(function ()
      {
        $(this).toggleClass('.tr');
      });
      $('#rc_desc$cid').click(function ()
      {
        $(location).attr('href','student.php?$cid');
      });
    });
  </script>

The issue is in the js/jquery. I want to be able to grab the $cid and pass it to the student.php page upon click. The php code above works but the js won't of course. I know the fundamental of client-side vs server-side languages. This question doesn't warrant a lecture. I know I cannot do this exactly, but it is what I want to happen ultimately. Any thoughts on how I can achieve this simply? Thanks in advance my friends!

问题出在 js/jquery 中。我希望能够获取 $cid 并在单击时将其传递给 student.php 页面。上面的 php 代码有效,但 js 当然不会。我知道客户端与服务器端语言的基础。这个问题不需要讲课。我知道我不能完全做到这一点,但这是我最终想要发生的事情。关于如何简单地实现这一目标的任何想法?先谢谢我的朋友们!

回答by Daniel Li

Yes, if you include the <script>section in your PHP code, you can do something similar to the following:

是的,如果您<script>在 PHP 代码中包含该部分,您可以执行类似于以下操作:

<script>
    var foo = <?php echo $foo; ?>;
</script>

In your case, you would be looking into the following code structure:

在您的情况下,您将查看以下代码结构:

<script>
    $(function () {
        $('#rc_desc<?php echo $cid ?>').hover(function () {
            $(this).toggleClass('.tr');
        });
        $('#rc_desc<?php echo $cid ?>').click(function () {
            $(location).attr('href', 'student.php?<?php echo $cid ?>');
        });
    });
</script>

The reason why this is possible is because although the Javascript is run on the client-side, it's processed on the server-side first prior to being presented on the page. Thus, it'll replace all necessary instances of $cidwith the one you have included.

之所以可以这样做,是因为尽管 Javascript 是在客户端运行的,但在呈现在页面上之前,它首先在服务器端进行了处理。因此,它将$cid用您包含的实例替换所有必要的实例。

Enjoy and good luck!

享受并祝你好运!

EDIT:

编辑

<script>
    $(function () {
        $('.rc_desc').hover(function () {
            $(this).toggleClass('.tr ');
        });
        $('.rc_desc').click(function () {
            $(location).attr('href', 'student.php?' + $(this).attr('id').split('rc_desc')[1]);
        });
    });
</script>

回答by Denys Séguret

You can do it like this :

你可以这样做:

    $(location).attr('href','<?php echo $cid ?>');

The javascript code doesn't know it comes from php, it appears as a constant (a literal) for it.

javascript 代码不知道它来自 php,它显示为它的常量(文字)。

回答by bretterer

Yes it is possible. All you have to do is put your <?PHP echo $cid; ?>in where you need it

对的,这是可能的。你所要做的就是把你<?PHP echo $cid; ?>的东西放在你需要的地方

<script>
$(function ()
{
  $('#rc_desc<?PHP echo $cid; ?>').hover(function ()
  {
    $(this).toggleClass('.tr');
  });
  $('#rc_desc$cid').click(function ()
  {
    $(location).attr('href','student.php?<?PHP echo $cid; ?>');
  });
});

This is possible because by the time the scrip is put into the page the cid has already been replaced by the string on the server. Since PHP is server driven, before it spits back the html/script it will be just like you put it in yourself.

这是可能的,因为在将脚本放入页面时,cid 已经被服务器上的字符串替换了。由于 PHP 是服务器驱动的,因此在它吐回 html/脚本之前,它就像您自己放入它一样。

回答by CrazyVipa

There is almost no way to actually communicate PHP and JavaScript. However, the best and simplest way is to set the ID within an attribute. The new HTML5 data attributes would be perfect.

几乎没有办法真正交流 PHP 和 JavaScript。但是,最好和最简单的方法是在属性中设置 ID。新的 HTML5 数据属性将是完美的。

For example, have a anchor tag with

例如,有一个锚标签

<span data-id="22">some event</a>

and then:

进而:

$(this).attr('href','student.php?'+$(this).attr('data-id'));

Or just use

或者只是使用

<?php echo $id; ?>

if it is not external JS file

如果不是外部JS文件

回答by Davuz

I think you block should be :

我认为你的块应该是:

<script>
    $(function ()
    {
    <?php foreach($courses as $cid=>cinfo) : ?>

      $('#rc_desc<?php echo $cid; ?>').hover(function ()
      {
        $(this).toggleClass('.tr');
      });
      $('#rc_desc<?php echo $cid; ?>').click(function ()
      {
        $(location).attr('href','student.php?<?php echo $cid; ?>');
      }); 
      ";?>
      <?php endforeach; ?>
    });
  </script>

UPDATE

更新

But you don't really need to do this, you have

但你真的不需要这样做,你有

"<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
"<span>Number of students</span>: $size<br/>".
"<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".

You can try this

你可以试试这个

$(function (){
      $('.rc_desc').hover(function ()
      {
        $(this).toggleClass('.tr');
      });
      $('.rc_desc').click(function ()
      {
        attrId = $(this).attr("data-id");
        $(location).attr('href','student.php?'+id);
      });
});

回答by kiranvj

Try this code

试试这个代码

foreach ($courses as $cid=>cinfo){ 
  $univ = $cinfo['univ'];
  $desc = $cinfo['desc'];
  $size = $cinfo['size'];
  $start = $cinfo['start'];
  print "<div class='rc_desc' data-id='$cid' id='rc_desc$cid'>"."$desc<br/>"."<b>$univ</b><br/>".
        "<span>Number of students</span>: $size<br/>".
        "<span>Started at</span>: ".date('F d, Y',strtotime($start))."<br/>".
}

<script>
    $(function ()
    {
      $('#rc_desc$cid').hover(function ()
      {
        $(this).toggleClass('.tr');
      });
      $('.rc_desc').click(function ()
      {
        $(location).attr('href','student.php?' + $(this).attr("data-id"));

       // if you want to redirect the page do this
       // window.location.href = 'student.php?' + $(this).attr("data-id");
      });
    });
  </script>