如何在PHP中使用JavaScript变量?

如何在PHP中使用JavaScript变量?

问题描述:

我正在尝试以下代码:

<script type="text/javascript">
for (i = 0; i < 5; i++) {
    for (x = 0; x < 1; x++) {
        $("#one" + i).html("<?php echo $arr["+i+"]["+x+"] ?>");
        $("#two" + i).html("<?php echo $arr["+i+"]["+x+1+"] ?>");
    };
};

</script>

未显示错误,但内容也未显示.

No error is showed, but content also not.

如何在PHP代码中使用JavaScript的增量变量?

How can I use the increment variable of JavaScript in PHP code?

谢谢

您可以使您的PHP数组可被JS访问(将其存储为js变量):

You may make your PHP-array accessible to JS(store it as a js-variable) :

<script type="text/javascript">
var arr=<?php echo json_encode($arr); ?>;
for (i = 0; i < arr.length; i++) {
    for (x = 0; x < 1; x++) {
        $("#one" + i).html(arr[i][x]);
        $("#two" + i).html(arr[i][x+1]);
    };
};
</script>