dtrpv60860 2014-08-26 06:32
浏览 16
已采纳

在php变量中检索javascript变量[重复]

This question already has an answer here:

I am trying to dynamically create variables in javascript. In my PHP code I already dynamically create variables from SQL output which gives me $res1, $res2, etc. Now I want to use these in my javascript but of course I cannot hardcode it for I can never know how many of the $res variables the PHP will generate.
I have the feeling I am already fairly close though. I have the following:

var i=1;
while (i<=count) {
    var name = "<?php ${"res".$_GET['i']} ?>";
}
</div>
  • 写回答

2条回答 默认 最新

  • dongtu4559 2014-08-26 06:38
    关注

    The php will be ran once after the server request, so $_GET[i] will only have one value for the entire loop. Which I'm presuming is not your desired effect.

    Try handling the variables on the backend and pushing them all into a array then sending that to the browser.

    for (var i = 0; i < data.length; i++) {
        var name = data[i];
    }
    

    where data is an array sent back from the server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器