Click here to Skip to main content
11,479,486 members (56,990 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML PHP
HI,

I want to access php function from javascript. this php function returns an array. I have to save it into a variable in javascript for further processing...
Kindly help me and guide me how i do that..

php code is:
<?php
 
 header("content-type: application/x-javascript");
 
$dirname="/myphp/MyPP";
$galleryarray = array();
 
    function returnimages($dirname) {
 
        $files = array();
 
        $curimage = 0;
 

        //valid image extensions

        $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";
 

        if($handle = opendir($dirname)) {
 

            while(false !== ($file = readdir($handle))) {
 
                if(eregi($pattern, $file)){
 
                  galleryarray[$curimage] =  $file';
 
                    $curimage++;
 
                }
 
            }
 

            closedir($handle);
 
        }
 

     return $galleryarray;
 

    }
 
    //Define array in JavaScript returnimages()
    //Output the array elements containing the image file names
 
  returnimages();
?>

html code is:

<script type="text/javascript">
 
            var galleryarray = returnimages();  /// ==> php function.. here i want to call php function

            var curimg = 0;
 
            function rotateimages(){
 

        var imagesDirectory =galleryarray[curimg];
 
                document.getElementById("slideshow").setAttribute("src", imagesDirectory)
 
                curimg = (curimg < galleryarray.length - 1) ? curimg + 1 : 0
            }
 
            window.onload = function(){
 
             setInterval("rotateimages()", 900)
 
            }
 
        </script>
Posted 31-Aug-12 0:25am
Misbah1536

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Javascript runs in the browser on the client machine, PHP code runs on the WWW server. The only way Javascript can communicate with PHP is by making a HTTP request. Look up AJAX[^].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Sergey Alexandrovich Kryukov 215
2 F-ES Sitecore 185
3 Richard MacCutchan 130
4 Frankie-C 104
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,441
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100