Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.