能否将PHP变量插入JS代码?

7

可能是重复问题:
如何从PHP向JavaScript传递变量

我正在动态生成一个列表。我希望在鼠标悬停时使每行变得可点击,并链接到一个页面。我想要在链接中传递该行内容的ID。

基本上:

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>

问题出在js/jquery文件中。我想要能够在点击时获取$cid并将其传递到student.php页面。上面的php代码可以运行,但是js无法实现。我知道客户端语言和服务器端语言的基础知识,这个问题不需要解释。我知道我不能完全实现这一点,但这是我最终想要发生的事情。有什么简单的方法可以实现吗?提前感谢我的朋友们!

以下建议解决了第一个问题。第二个问题是函数需要动态地与列表一起工作。目前我的JS只能在最后一个项目上运行...有什么想法吗? - campatsky
6个回答

10

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

<script>
    var foo = <?php echo $foo; ?>;
</script>
在你的情况下,你需要查看以下代码结构:
<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>
这种可能性的原因是,尽管 JavaScript 是在客户端运行的,但它首先在服务器端进行处理,然后再呈现在页面上。因此,它将用你包含的那个取代所有必要的 $cid 实例。
祝你好运!享受它吧!
编辑:
<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>

啊!它似乎不能动态地为列表中的所有项目工作。只有最后一个加载的项目能够工作。 - campatsky
@campatsky 答案已更新,请尝试。 - Daniel Li

5
你可以这样做:
    $(location).attr('href','<?php echo $cid ?>');

JavaScript代码不知道它来自PHP,对于它而言,它只是一个常量(文字字面值)。


2

是的,这是可能的。您只需将<?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; ?>');
  });
});

这是因为当脚本放入页面中时,cid已经被服务器上的字符串替换了。由于PHP是服务端驱动的,在它将html/script传回之前,它会像您自己输入一样呈现。


1

几乎没有办法直接通信PHP和JavaScript。然而,最好和最简单的方法是在属性中设置ID。新的HTML5数据属性非常适合。

例如,使用一个锚点标签:

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

然后:

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

或者直接使用

<?php echo $id; ?>

如果它不是外部JS文件


1

我认为你的阻止应该是:

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

更新

但实际上你并不需要这样做,你已经有了

"<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/>".

你可以试试这个

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

1

尝试这段代码

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接