1
votes

I have an array that was populated by this script :

var imgs = document.getElementsByTagName("img");
var imgSrcs = [];

for (var i = 0; i < imgs.length; i++) {
    imgSrcs.push(imgs[i].src);
}

When I echo this out from a PHP, it gives me all the img src with commas separating them. It seems like its not like a typical array format, more like just text. When I tried to do this:

for ($i = 0; $i < count($img); ++$i) {
    echo "<img src='".$img."'/><br>";
}

It just echo's one <img src with every single image src that is in imgSrcs.

I'm trying to echo img src and 1 img link. This way if I have 5 links in imgSrcs, i'll output 5 images.

Thanks in advance!

1
how are you passing the value of the array to php? therein lies your answer. - Sean Johnson
using ajax and into a php file $(isset($_POST['img'])) - hellomello
where in ajax data: "img="+imgSrcs - hellomello
And I dont think that matters, because when I just do alert(imgSrcs) it outputs exactly what I'm describing: list of img sources with comma separated - hellomello
With "img="+imgSrcs you're trying to concatenate your array onto a string, which does a .toString() on imgSrcs resulting in a comma-separated string of the values from the array. Similarly, alert() is used to display strings, so alert(imgSrcs) displays your data with commas because it does a .toString() on your array. - nnnnnn

1 Answers

1
votes

Try explode to get your array back, if you're passing it as a comma delimited list.

$img = explode(",",$img);
for ($i = 0; $i < count($img); ++$i) {
    echo "<img src='".$img."'/><br>";
}