简体   繁体   English

通过$ _GET将URL变量传递给Javascript

[英]Passing URL variable via $_GET to Javascript

I have a PHP page with the following URL: 我有一个带有以下URL的PHP​​页面:

http://xyz.com/dev/gallery.php?k=1&s=35

The following Javascript file is included on the PHP page: PHP页面上包含以下Javascript文件:

<script type="text/javascript" src="js/some-js-file.php"></script>

The some-js-file.php needs to be filled dynamically using PHP: 需要使用PHP动态填充some-js-file.php:

jQuery(function($){$.supersized({

<?php 
$sql = "SELECT * FROM tbl WHERE id=".$_GET['s']." ORDER BY pos ASC"; 
$result = mysql_query($sql); 
    while($rs = mysql_fetch_row($result)){
    ...do some output here...
    }
?>

});
});

For some reason, $_GET['s'] is empty. 由于某些原因,$ _ GET ['s']为空。 Any ideas why? 有什么想法吗? Thanks in advance for any help! 在此先感谢您的帮助!

Pass the parameter to the JavaScript file as well: 还将参数传递给JavaScript文件:

<script src="js/some-js-file.php?s=<?php print $_GET['s']; ?>"></script>

NB: Please pay attention to Quentin's comments and make sure that you use the approach securely. 注意:请注意Quentin的评论 ,并确保您安全地使用该方法。

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

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