Results 1 to 5 of 5

Thread: Adding PHP code to Javascrip

  1. #1
    Join Date
    Jan 2014
    Location
    East Tennessee
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding PHP code to Javascrip

    I am using the fadeimages plugin and I want to be able to dynamically build the array instead of having it static. I have created a PHP file that builds the array by reading the contents of a pictures directory. My question is how to include this code into the javascript where the array would normally be. Here is the php file:

    <?php // Build fadepic array
    $x=0;
    foreach (glob("../main_page_images/*.*") as $filename) {
    $filename = substr($filename, 20);
    echo "fadeimages[".$x."]=[\"main_page_images/".$filename."\", \"\", \"\"]<br>";
    $x++; ?>

    And of course I want to put it where the array would be. I have been trying to use json but can't get it to work properly. Any help would be greatly appreciated. Thanks!

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    If I'm understanding correctly, you could put the JavaScript internally in the web page and echo the PHP into it - very simply;
    Code:
    <html>
    <head>
    
    <script>
    ... script stuff ...
    
    <?php // Build fadepic array
     $x=0;
     foreach (glob("../main_page_images/*.*") as $filename) {
     $filename = substr($filename, 20);
     echo "fadeimages[".$x."]=[\"main_page_images/".$filename."\", \"\", \"\"]<br>";
     $x++; ?>
    
    ... more script stuff ...
    </script>
    
    </head>
    <body>
    
    ... HTML stuff ...
    
    </body>
    </html>
    This will only work with the JavaScript located directly into the web page. It wouldn't work if the JavaScript is in an external file (js client-side / php server-side differences).

    If you need more help, please provide a link to the script demo page here on DD and also a link to your page.
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Jan 2014
    Location
    East Tennessee
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I wish that worked... I must be overlooking something. You can see the test file at http://www.kimwatt.org/index_test.php. You will be able to see I have the php code embedded and the javascript is on the same page as you say. I know the php code works because you can run it alone by going to http://www.kimwatt.org/build_array.php and it outputs the array just as it is needed. you can see the actual array if you view the source on the site's index page. I am trying to get it so I can just drop .jpg files in a directory and the array builds dynamically. Thank you very much for your help!

  4. #4
    Join Date
    Jan 2014
    Location
    East Tennessee
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    viewing the source I see now why it's not working. it is adding a /s after the / in the array. Here is a element it printed:
    fadeimages[0]=["main_page_images/s/fade_pic_2.jpg", "", ""]
    The /s should not be there. How can I get rid of it?

  5. #5
    Join Date
    Jan 2014
    Location
    East Tennessee
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I got it working!!! i had to put a \n to send a newline in the array. Also the /s is because I didnt have the substr set to the proper length. (its been a long day). Thanks for your help!

Similar Threads

  1. Adding PHP code into wordpress widgets
    By brainsmith in forum PHP
    Replies: 3
    Last Post: 12-04-2012, 05:48 AM
  2. Autoclose on Jquery/Javascrip Popup Box
    By ccscreative in forum JavaScript
    Replies: 2
    Last Post: 09-07-2011, 11:23 PM
  3. help with javascrip calenders/booking
    By iflyjetz in forum JavaScript
    Replies: 5
    Last Post: 12-15-2009, 04:03 AM
  4. Adding a chunk of code
    By hmsnacker123 in forum Looking for such a script or service
    Replies: 1
    Last Post: 06-18-2008, 11:53 PM
  5. Resolved Adding persistance cookie to code
    By bigalo in forum JavaScript
    Replies: 5
    Last Post: 02-27-2008, 03:23 PM

Bookmarks

Posting Permissions

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