简体   繁体   English

Jquery/ Javascript 从 PHP scope 问题获取信息?

[英]Jquery/ Javascript getting information from PHP scope issue?

I am trying to get some information from our database and then use it in javascript/JQuery and I think I might be doing something wrong with the scope of the coding.我试图从我们的数据库中获取一些信息,然后在 javascript/JQuery 中使用它,我想我可能对编码的 scope 做错了什么。

Here is the current segment of code on my phtml page (magento)这是我的 phtml 页面上的当前代码段(magento)

<script type="text/javascript">
<?php
echo 'var $image-paths = new Array();';
for ($i = 0; $i < count ($_child_products); $i++)
{
    echo '$image-paths[';
    echo $i;
    echo '] = ';
    echo $this->helper('catalog/image')->init($_child_products[$i], 'image');
    echo ';';
}
?>
document.getElementById('main-image').href = $image-paths[1];
</script>

The bottom getElementById is just for testing to see if it really grabbed that image path.底部的 getElementById 只是为了测试它是否真的抓住了那个图像路径。 So far the php stuff is working and echo'ing the correct links.到目前为止,php 的东西正在工作并回显正确的链接。 However, is simply echo'ing them not enough;然而,仅仅回应它们是不够的; does it actually register it into the javascript code?它实际上是否将其注册到 javascript 代码中?

Here is what my source code looks like on my server:这是我的源代码在我的服务器上的样子:

<script type="text/javascript">
var $image-paths = new Array();
$image-paths[0] = http://staging.greencupboards.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5f  b8d27136e95/feeds/MrsMeyers/MRM-64565-a.jpg;
$image-paths[1] = http://staging.greencupboards.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/feeds/MrsMeyers/MRM-64566-a.jpg;
$image-paths[2] = http://staging.greencupboards.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/feeds/MrsMeyers/MRM-64568-a.jpg;
$image-paths[3] = http://staging.greencupboards.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/feeds/MrsMeyers/MRM-D43114-a.jpg;
document.getElementById('main-image').href = $image-paths[1];
</script>

But the image link does not change to image-path[1].但是图像链接不会更改为图像路径[1]。 Any ideas?有任何想法吗?

Thanks in advance!提前致谢!

$image-paths[0] = http://staging.greencupboards.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5f  b8d27136e95/feeds/MrsMeyers/MRM-64565-a.jpg;
                  ^-- no quote here, or at the end of the string

You're producing invalid javascript.您正在生产无效的 javascript。 Pop up your javascript console (shift-ctrl-J in chrome/firefox) and you'll see the error.弹出您的 javascript 控制台(chrome/firefox 中的 shift-ctrl-J),您会看到错误。

Producing javascript dynamically is problematic.动态生成 javascript 是有问题的。 Anytime you insert something from a PHP variable/function, you should run that through json_encode(), which guarantees you get valid javascript:任何时候你从 PHP 变量/函数中插入一些东西,你应该通过 json_encode() 运行它,这保证你得到有效的 javascript:

echo json_encode($this->helper('catalog/image')->init($_child_products[$i], 'image'));

Or better yet, change the code to:或者更好的是,将代码更改为:

$links = array();
for ($i = 0; $i < count ($_child_products); $i++)
    $links[] = $this->helper('catalog/image')->init($_child_products[$i], 'image');
}

echo '$image-paths = ', json_encode($links);
<script type="text/javascript">
<?php
echo 'var $image_paths = new Array();';
for ($i = 0; $i < count ($_child_products); $i++)
{
    echo '$image_paths[';
    echo $i;
    echo '] = "'; // Here the starting of quotes.
    echo $this->helper('catalog/image')->init($_child_products[$i], 'image');
    echo '";'; // Here the ending of quotes.
}
?>
document.getElementById('main-image').href = $image_paths[1];
</script>

This should work now.现在应该可以了。 Hope it helps.希望能帮助到你。

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

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