1

i am trying to create a fade effect with the following...ive been told im nearly there apart from the passing of the json array. At the moment no images are displayed.

 //generate all the boxes
$.get('images.php',function(data){
  for (var i=0; i < totalBoxes; i++){
      var randomImage = data[Math.floor(Math.random() * data.length)];
      $('<div class="pf-box"><img class="black" src="' + randomImage['black'] + '" /><img class="colour" src="' + randomImage['colour'] + '" /></div>').hide().appendTo('#bg').fadeIn('slow').filter('.colour').css("opacity", 0);
  }
 },'json');

 //add the hover behavior to all the elements
 $('.colour').hover(function() {
   $(this).stop().fadeTo(700, 1);
 },function() {
   $(this).stop().fadeTo(700, 0);
 });

and images.php

    <?php 
   header('Content-type: application/json');
echo '[  
    {'black' : 'images/random/1.jpg', 'colour' : 'images/random/1-c.jpg'},  
    {'black' : 'images/random/2.jpg', 'colour' : 'images/random/2-c.jpg'}
]';
    ?>

3 Answers 3

2

Don't you need to escape the quotes inside the JSON string? Otherwise the php interpreter will fail to send all of what you want, and may even barf out some errors.

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to create this effect: yellostudio.co.uk/temp/index.php with the code above on yellostudio.co.uk/temp/indexV2.php Do you know where i am going wrong?
0

Use randomImage.black instead of randomImage['black']

16 Comments

there's no difference between the two in JavaScript
there is no official support for associative arrays in JavaScript.. give those points back
no problem, I forgot the "!" telling that I wasn't really mad.. :) here's a reference: hunlock.com/blogs/Mastering_Javascript_Arrays
"i didnt know i had removed any points sorry" Don't bother, I think it's Mr. Haboustak
No probs Mike i appreciate your help...do you know how i could get teh hover working?
|
0

Your echo is failing because of the single quotes in the JSON output breaking out of the echo.

Enclose your string with different quotes so you can echo properly:

<?php
header('Content-type: application/json'); 
echo "[
    {'black' : 'images/random/1.jpg', 'colour' : 'images/random/1-c.jpg'},
    {'black' : 'images/random/2.jpg', 'colour' : 'images/random/2-c.jpg'} 
]"; 
?>

Note the use of double quotes to enclose the echo string, instead of the single quotes you've used. (if you had double quotes inside the string, then you'd reverse it and use single on the outside).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.