简体   繁体   English

如何通过 url 传递要插入 JS 脚本 url 中的 PHP 变量

[英]How to pass via url a PHP variable to be inserted in a JS script url

I need to pass a couple of PHP variables via url.我需要通过 url 传递几个 PHP 变量。 These variables need to be inserted in a JS script which loads a script.这些变量需要插入到加载脚本的 JS 脚本中。

I have this code on the HTML (.php) page which loads the script:我在加载脚本的 HTML (.php) 页面上有这段代码:

<script type="text/javascript" src="https://www.example.com/index.php?controller=GzFront&action=load&cid=1&layout=calendar&show_header=F&eid=6262&group=1&local=4"></script>

I need to pass the variables "eid" (number) and "group" (number) via URL from another page.我需要从另一个页面通过 URL 传递变量“eid”(数字)和“group”(数字)。

How can I do this?我怎样才能做到这一点? Should I use a url like www.domain.com/scriptpage.php?eid=6262&group=1 ?我应该使用像www.domain.com/scriptpage.php?eid=6262&group=1这样的 url 吗? And then, how can I put these 2 variables in the script url?然后,如何将这两个变量放入脚本 url 中?

I'd like to use one script/one page to show different contents depending on the variables passed via URL from a HTML (.php) page.我想使用一个脚本/一页来显示不同的内容,具体取决于从 HTML (.php) 页面通过 URL 传递的变量。

Solved this way:以这种方式解决:

add the variable to the url using?:将变量添加到 url 使用?:

pagename.php/?var1=par1&var2=par2

in the script url add:在脚本网址中添加:

<?php echo $_GET['var1']; ?>
<?php echo $_GET['var2']; ?>

where you need the variable.你需要变量的地方。

https://www.example.com/index.php?eid=<?php echo $_GET["eid"]

that's it!而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM