Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I retrieve image paths with the function get_all();. This get_all function retrieves images as an object. This object has the attributes name, source_path and date. I want my javascript to add images to a div. I have the following:

The instantiate.php includes files like Jquery and another JS file.

<?php
    require_once("../../include/instantiate.php");    
    $photos = Photos::get_all();
    $JSPhotos;
    foreach($photos as $photo) { $JSPhotos + $photo->source_path; }
?>

<script type="text/javascript">
    $(document).ready(function() {
        var photos = <?php echo json_encode($JSPhotos); ?>;
        for(var i = 0; i <= 10; i++)
        {
            create_image("../"+photos[i]);
        }
    });

This does not work. Anyone got a solution? Solution in Jeroen's Post!

New Problem;

In the create_image function I set the class and src of the image element. When you click such an image I want an alert box to show up. I have checked if the class is set correctly, and I concluded that all images did have the classname "imgid". So, any idea why this dont work?

Script in the javascript part:

$(".imgid").click(function() {
    alert("hey");
});
share|improve this question
    
whats the error you're getting? –  bountyh Mar 11 at 21:33
    
Appearently the variable "photos" in the javascript part is "null". –  user2014780 Mar 11 at 21:34
    
thats because your just doing $JSPhotos +. theres nothing assigned to the variable –  bountyh Mar 11 at 21:35
    
You should pass an actual object-graph to json_encode - what is $JSPhotos + $photo->source_path supposed to do? It's sure not creating an array. –  user2864740 Mar 11 at 21:36
2  
@user2014780 Perhaps, although it might also include too much data. There isn't a problem with (and I actually recommend doing so) creating an intermediate structure .. if it's actually created correctly. –  user2864740 Mar 11 at 21:37

1 Answer 1

up vote 1 down vote accepted

You are not assigning anything to your variable.

You probably want:

$JSPhotos = array();
foreach($photos as $photo) {
  $JSPhotos[] = $photo->source_path;
}

Or something similar.

share|improve this answer
    
Thanks. I have done exactly that, but unfortunately I get broken images now. I have checked the contents of the photos variable in JS and its all "null". –  user2014780 Mar 11 at 21:43
    
@user2014780 Check your $photos and $JSPhotos variables. What do they contain (do a var_dump())? –  jeroen Mar 11 at 21:45
    
Thanks, it works now. Can't remember what the problem was though (short memory). But right now I have created a function that doesnt work. Can you please check the OP? I am sure you solve it in a second haha! –  user2014780 Mar 11 at 22:27
    
@user2014780 You would need to show the generated html or the complete function. Either way, I would recommend posting a new question, preferably with an online example to test with. –  jeroen Mar 11 at 22:32
    
@user2014780 By the way, you probably need event delegation as the elements are probably not yet added to the page when you add your click-handler. –  jeroen Mar 11 at 22:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.