www.webdeveloper.com
Results 1 to 3 of 3

Thread: Can't seem to get my JavaScript to get a value from PHP

  1. #1
    Join Date
    Jan 2013
    Posts
    1

    Unhappy Can't seem to get my JavaScript to get a value from PHP

    Rightio, I need to count the number of files in a directory then assign that number to a variable in my website. Here is what I have thus far.

    PHP Script to count files
    PHP Code:
    $directory "../comics/";
    if (
    glob($directory "*.png") != false)
    {
     
    $filecount count(glob($directory "*.png"));
     echo 
    $filecount;
    }
    else
    {
     echo 
    0;

    And here is my javascript which is inside my html page
    Code:
    <script type="text/javascript">
    var numofImages = "<?=$filecount?>";
    var random_img = new Array();
    for(var i=0;i<numofImages-1;i++) { 
       random_img[i] = '<a href="comics/'+i+'.png"><img src="comics/'+i+'.png"; width="260" height="320" border="0" alt=""></a>';
    }
    
    var random_number = Math.floor((Math.random()*random_img.length));
    random_img[random_number];
    document.write(random_img[random_number]);
    </script>
    Now, my problem is this, it wont get the file count. If I set numofImages to an actual number the entire thing works perfectly fine. But for some reason it doesn't seem to get the file count. Any tips?

    Cheers.

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    753
    Not knowing about your personal PHP/Javascript knowledge or how the page(s) are set up I'll first just toss in a little note to make sure that your script code itself is on a .php page and also obviously the same page that sets the $filecount variable.

    Silly things out of the way, have you tried:
    Code:
    <script type="text/javascript">
    var numofImages = "<?php echo $filecount; ?>";
    var random_img = new Array();
    for(var i=0;i<numofImages-1;i++) { 
       random_img[i] = '<a href="comics/'+i+'.png"><img src="comics/'+i+'.png"; width="260" height="320" border="0" alt=""></a>';
    }
    
    var random_number = Math.floor((Math.random()*random_img.length));
    random_img[random_number];
    document.write(random_img[random_number]);
    </script>
    I suppose I could have looked it up quickly, but I have never seen the format <?=$variable?> used to insert PHP in to a page. Generally I always set a proper opening and closing PHP tag and then simply echo out any data I want printed in the page's source.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    Quote Originally Posted by Sup3rkirby View Post
    I suppose I could have looked it up quickly, but I have never seen the format <?=$variable?> used to insert PHP in to a page.
    It is valid if that feature is enabled on the server, but it is indeed generally better to use proper opening and closing php tags and use echo.
    New to web development or in need of a good reference? Check out the Mozilla Developer Network or W3Schools.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles