简体   繁体   English

如何将php数组字符串作为参数传递给javascript函数?

[英]How to pass php array string to javascript function as a parameter?

I am iterating through a php array to display a list of photo filters. 我正在遍历php数组以显示照片滤镜列表。 I need to be able to pass the selected filter name to my javascript function to load it, for now I am only able to get the $i id but the full filter name would be so much more easier. 我需要能够将选定的过滤器名称传递给我的javascript函数以加载它,目前,我只能获得$i id,但是完整的过滤器名称将更加容易。

How can pass $filters[$i] to getFilterName() ? 如何将$filters[$i]传递给getFilterName()

<?php
    for ($i = 0; $filters[$i]; $i++)
    {
        if (strpos($filters[$i], '.png'))
        {
            ?>
            <!-- get php variable for javascript : -->
            <div id="filter-target" style="display: none;">
                <?php
                    echo htmlspecialchars($i);
                ?>
            </div>
            <!-- ... -->

            <?php
            echo '<div tabindex="-1" class="filter_box" onclick="getFilterName('.$i.')">';
                echo '<img class="filter" src="pictures/filters/'.$filters[$i].'"/>';
            echo '</div>';
        }
    }
?>

Thank you! 谢谢!

Because the filename is a string, you need to escape it in ' ' like so: 由于文件名是一个字符串,因此您需要像这样在' '对其进行转义:

<?php
    for ($i = 0; $filters[$i]; $i++)
    {
        if (strpos($filters[$i], '.png'))
        {
            ?>
            <!-- get php variable for javascript : -->
            <div id="filter-target" style="display: none;">
                <?php
                    echo htmlspecialchars($i);
                ?>
            </div>
            <!-- ... -->

            <?php
            echo '<div tabindex="-1" class="filter_box" onclick="getFilterName(\''.$filters[$i].'\')">';
                echo '<img class="filter" src="pictures/filters/'.$filters[$i].'"/>';
            echo '</div>';
        }
    }
?>

Otherwise it would treat the string as a variable name in JavaScript and wouldnt fint it because it doesnt exist. 否则,它将在JavaScript中将字符串视为变量名,并且不会查找它,因为它不存在。

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

相关问题 如何将PHP数组参数传给Javascript Function? - How to pass PHP array parameter to Javascript Function? 如何在javascript参数函数中传递php post方法字符串? - how to pass php post method string in javascript parameter function? 如何从PHP端传递JavaScript函数中的数组字符串作为参数? - how to pass array string in JavaScript function from PHP end as a argument? 如何将JSON数组传递给php中字符串中的javascript函数 - How to pass a JSON array to a javascript function inside a string in php 如何将php变量作为参数传递给JavaScript函数 - How to pass php variable to JavaScript function as parameter 如何在JavaScript代码函数中将数组作为参数传递? - How to pass array as parameter in javascript code function? 将php字符串作为参数传递给php脚本中调用的javascript函数 - Pass php string as parameter for javascript function called in php script 当整数,字符串和数组作为JavaScript中的函数参数传递时,有什么区别 - What is the difference when a integer, string and array pass as function parameter in javascript 如何将PHP数组传递给JavaScript函数? - How to Pass a PHP array to JavaScript function? Javascript:如何将带有字符串参数的函数作为参数传递给另一个函数 - Javascript: How to pass a function with string parameters as a parameter to another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM