Jump to content

set a php variable in javascript code?


wmguk

Recommended Posts

hey guys, Im trying to set a variable from php in to a javascript code, however it just falls over everytime :(

 

this is the contents of getpics.php and is linked to the page via :

 

<script src="../images/getpics.php" type="text/javascript"></script>

 

All i am trying to do is set function returnimages($dirname=".") to be function returnimages($dir) and then $dir is assigned on the original page

 

<?
Header("content-type: application/x-javascript");

function returnimages($dirname=".")
{
   $pattern="\.(jpg|jpeg|png|gif|bmp)$";
   $files = array();
   $curimage=0;
   if($handle = opendir($dirname)) {
       while(false !== ($file = readdir($handle))){
               if(eregi($pattern, $file)){
	 $filedate=date ("M d, Y H:i:s", filemtime($file));
                 echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'"];' . "\n";
                 $curimage++;
               }
       }

       closedir($handle);
   }
   return($files);
}

echo "var galleryarray=new Array();" . "\n";
returnimages();
?> 

 

any ideas?

Link to comment
https://forums.phpfreaks.com/topic/96506-set-a-php-variable-in-javascript-code/
Share on other sites

change link to

<script src="../images/getpics.php?dir=bla" type="text/javascript"></script>

and sctript to

<?
Header("content-type: application/x-javascript");
$dirname=$_GET['dir'];
function returnimages($dirname=".")
{
   $pattern="\.(jpg|jpeg|png|gif|bmp)$";
   $files = array();
   $curimage=0;
   if($handle = opendir($dirname)) {
       while(false !== ($file = readdir($handle))){
               if(eregi($pattern, $file)){
	 $filedate=date ("M d, Y H:i:s", filemtime($file));
                 echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'"];' . "\n";
                 $curimage++;
               }
       }

       closedir($handle);
   }
   return($files);
}

echo "var galleryarray=new Array();" . "\n";
returnimages();
?> 

change link to

<script src="../images/getpics.php?dir=bla" type="text/javascript"></script>

and sctript to

<?
Header("content-type: application/x-javascript");
$dirname=$_GET['dir'];
function returnimages($dirname=".")
{
   $pattern="\.(jpg|jpeg|png|gif|bmp)$";
   $files = array();
   $curimage=0;
   if($handle = opendir($dirname)) {
       while(false !== ($file = readdir($handle))){
               if(eregi($pattern, $file)){
	 $filedate=date ("M d, Y H:i:s", filemtime($file));
                 echo 'galleryarray[' . $curimage .']=["' . $file . '", "'.$filedate.'"];' . "\n";
                 $curimage++;
               }
       }

       closedir($handle);
   }
   return($files);
}

echo "var galleryarray=new Array();" . "\n";
returnimages();
?> 

hey thank you, I did try this, but it failed, i just did it again, and did an echo of $dir, that shows right... but it still doesnt get the right images, its still looking in "." rather than $dir...

 

any thoughts?

This is something AJAX was specifically designed for.

 

Create a php script that outputs xml data, then call it in to javascript with an AJAX function.

 

Plenty of google tutorials for both steps

 

thats cool, i havent even started on AJAX yet, i know ive got to really, but its bad enough getting my head round php lol :)

scrub that, got it sorted thank you :)

 

now i need to get the variable title out of this code into php

 

function buildimage(i){
var imagecompletepath=(targetlinkdir!="")? targetlinkdir+galleryarray[i][0] : imagepath+galleryarray[i][0]
var tempcontainer='<a href="'+imagecompletepath+'" target="'+href_target+'" onClick="return popuplinkfunc(this)">'
tempcontainer+='<img src="'+imagepath+galleryarray[i][0]+'" title="'+galleryarray[i][0]+'"/>'
tempcontainer+='</a><br />'
tempcontainer+=(descriptionprefix[0]==1)? descriptionprefix[1]+(i+1) : ""
return tempcontainer
}

 

specifically looking at

title="'+galleryarray[i][0]+'"

I have already tried <? $name = '+galleryarray[0]+' ; ?> however as expected i just get the '+galleryarray[0]+' rather than the actual item...

 

any thoughts?

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.