Jump to content

php in javascript


justAnoob

Recommended Posts

I found a little javascipt online, and I wanted to play around with it.  I'm trying to add a little php to it, I think I'm close to getting it, but when I click on the properties of the pictures that I want displayed, it has a bunch of extra garbage in it.  So is the javascript reading something extra in the php script ?  Here is the code.

<script language="JavaScript">
  var j,d="",l="",m="",p="",q="",z="",KW_ARI= new Array()
  
  KW_ARI[KW_ARI.length]='<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>';
  KW_ARI[KW_ARI.length]='<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>';
  KW_ARI[KW_ARI.length]='<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>';
  KW_ARI[KW_ARI.length]='<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>';
  KW_ARI[KW_ARI.length]='<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>';
  j=parseInt(Math.random()*KW_ARI.length);
  j=(isNaN(j))?0:j;
    document.write("<img name='seqSlideShow' src='"+KW_ARI[j]+"'>");
function seqSlideShow(t,l) {
  x=document.seqSlideShow; j=l; j++;  if (j==KW_ARI.length) j=0;
  x.src=KW_ARI[j]; setTimeout("seqSlideShow("+t+","+j+")",t);
}
seqSlideShow(10000,0)
</script>

Link to comment
https://forums.phpfreaks.com/topic/218490-php-in-javascript/
Share on other sites

First off, it's much easier to define an array in JS like:

 

  var KW_ARI = [
    '<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>',
    '<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>',
    '<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>',
    '<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>',
    '<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>'
  ];

 

(Remove "KW_ARI= new Array()" too)

 

Secondly, what extra garbage?

Link to comment
https://forums.phpfreaks.com/topic/218490-php-in-javascript/#findComment-1133440
Share on other sites

You're defining part of the image tag within the array, and then again within the document.write calls:

 

KW_ARI[KW_ARI.length]='<?php echo '<img src="trackimages/' . $row['new_pic'] . '" />'; ?>';

document.write("<img name='seqSlideShow' src='"+KW_ARI[j]+"'>");
Link to comment
https://forums.phpfreaks.com/topic/218490-php-in-javascript/#findComment-1133465
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.