简体   繁体   English

将PHP变量转换为Javascript变量,并将该Javascript变量用作Javascript函数中的参数

[英]Turn PHP variable into Javascript variable and use that Javascript variable as an argument in a Javascript function

An input from a form in my index.php page is sent to my search.php page. 来自index.php页面中表单的输入被发送到search.php页面。 This input is turned into the PHP variable $q with $_GET. 此输入通过$ _GET转换为PHP变量$ q。 How do I turn this PHP variable into a Javascript variable that is a string? 如何将此PHP变量转换为字符串的Javascript变量? Then how do I pass this string as an argument in a Javascript function in the body tag when the page loads? 那么,当页面加载时,如何在body标签的Javascript函数中将此字符串作为参数传递呢? The onload function only works if the first parameter is a string. 仅当第一个参数是字符串时,onload函数才起作用。 Here is my simplified code: 这是我的简化代码:

<?php

if(isset($_GET["q"])) {
    $q = $_GET["q"];
}

?>

<!DOCTYPE html>
<html>
<head>
 <script src="assets/js/script.js">
     var q = "<?php echo $q;?>";
 </script>
</head>

<body onload="return Search(*PHP variable turned into Javascript string*, otherFunction(), otherFunction2())">

    <div>
    Content
    </div>

</body>
</html>

Javascript within your script tag 'should' be ignored if there's a src for it. 如果脚本标签中有一个src,则应“忽略”该脚本中的Javascript。 However you can't have precompiled php in a js file. 但是,您不能在js文件中预编译php。 One option is to set the javascript variables within your php files, then separately include your js files. 一种选择是在php文件中设置javascript变量,然后分别包含js文件。 If your js files make use of those variables, make sure to set the variables first: 如果您的js文件使用了这些变量,请确保首先设置变量:

<script type="text/javascript">
    var q = "<?php echo $q;?>";
</script>
<script src="assets/js/script.js"></script>

The script doesn't work because you're trying to include an external JavaScript file, while also using the same tag for inline script. 该脚本不起作用,因为您试图包含一个外部JavaScript文件,同时对嵌入式脚本也使用相同的标记。

This part 这部分

<script src="assets/js/script.js">
    var q = "<?php echo $q;?>";
</script>

Should look something like this 应该看起来像这样

<script src="assets/js/script.js"></script>
<script>
    var q = "<?php echo $q;?>";
</script>

Fully fixed code 完全固定的代码

<?php

if(isset($_GET["q"])) {
    $q = $_GET["q"];
}

?>

<!DOCTYPE html>
<html>
<head>
    <script src="assets/js/script.js"></script>
    <script>
        var q = "<?php echo $q;?>";
    </script>
</head>

<body onload="return Search(q, otherFunction(), otherFunction2())">

    <div>
    Content
    </div>

</body>
</html>

Edit: Footnote 编辑:脚注

As @dossy points out, you should avoid directly passing user input into the HTML code. 正如@dossy指出的那样,您应该避免将用户输入直接传递到HTML代码中。 Use json_encode() to avoid any malicious code or invalid input. 使用json_encode()避免任何恶意代码或无效输入。

<script>
    var q = <?php echo json_encode($q); ?>;
</script>

This is what json_encode() is for. 这就是json_encode()目的。 Other answers that use the naive approach of var x = "<?php echo $var; ?>"; 使用var x = "<?php echo $var; ?>";天真方法的其他答案var x = "<?php echo $var; ?>"; will break if $var contains a double quote, for example. 例如,如果$var包含双引号,则将中断。

This is the safest way of doing this: 这是最安全的方法:

<script type="text/javascript">
  var q = <?php echo json_encode($q); ?>;
</script>

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

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