-6

I have been trying to figure this out. The image path and href are php variables.

echo'<a href="'. $bigImg[0]->src .'"><img src="'. $thumbImg[0]->src .'" alt="" /></a>';
echo'<a href="'. $bigImg[1]->src .'"><img src="'. $thumbImg[1]->src .'" alt="" /></a>';

I am trying to turn the php variables into a javascript.

The PHP variables are:

'. $bigImg[0]->src .'
'. $thumbImg[0]->src .'

How can I pass this into javascript?

Jaz
  • 1
  • 1

2 Answers2

0

This will only work if your page is generated by PHP

you would start off with

This is all inside a PHP file, lets call it index.php

<?php
// These are your arrays which contain your objects that can call their src public member.
$bigImg = array();
$thumbBig = array(); 

?>

<html>
    <body>
        <script>
            var bigImgSrc = <?php echo json_encode($bigImg[0]->src); ?>;
            var thumbBigSrc = <?php echo json_encode($thumbImg[0]=>src); ?>;
        </script>
    </body>
</html>
kchang4
  • 92
  • 5
-1

If I've understood correctly.. i would do something like:

<script>
var bigSrc='<?=$bigImg[0]-src;?>';
var thumbSrc='<?=thumbImg[0]->src;?>';
</script>

then, in the appropriate place

document.write("<a href="'+bigSrc+"'><img....</a>")

or via Jquery

<script>
$(document).ready(function(){
$("<a href="'+bigSrc+"'><img....</a>").appendTo(parentnode..)
})
</script>
user1555320
  • 485
  • 2
  • 8
  • 1
    Not properly esacped, your quotes are imbalanced, and you’re missing a `>`. `document.write` is a bad idea. – Ry- Jan 08 '14 at 18:00
  • so whats in between img ... – Jaz Jan 08 '14 at 18:04
  • @minitech: why should document.write be a bad idea? I've proposed two different approach it's up to the OP to chose the one hat best fits its needings. jaz, sorry for the unbalanced quotes; as you may have seen it should be $("....").appendTo(parentnode..) – user1555320 Jan 13 '14 at 10:27